public TeamFoundationJobExecutionResult Run( TeamFoundationRequestContext requestContext, TeamFoundationJobDefinition jobDefinition, DateTime queueTime, out string resultMessage) { resultMessage = string.Empty; try { this.LogInfo(string.Format("Enter Job")); lock (JobLock) { XmlNode xmlData = jobDefinition.Data; object notificationEventArgs = DeserializeXml(xmlData); this.workflowRunner.ProcessEvent(requestContext, NotificationType.Notification, notificationEventArgs, true, true); } } catch (RequestCanceledException e) { this.LogError(string.Format("Workflow cancel: "), e); return(TeamFoundationJobExecutionResult.Stopped); } catch (Exception e) { this.LogError(string.Format("Workflow error: "), e); resultMessage = e.ToString(); return(TeamFoundationJobExecutionResult.Failed); } this.LogInfo(string.Format("Leave Job")); return(TeamFoundationJobExecutionResult.Succeeded); }
private void ReadJobSchedule(string jobId) { var job = jobs[jobId.ToUpper()]; var jobType = job.Item2; if (jobType.IsDefined(typeof(JobContextAttribute))) { var context = jobType.GetCustomAttribute <JobContextAttribute>().Context; switch (context) { case JobContext.Server: if (!rbServer.Checked) { rbServer.Enabled = true; rbServer.Checked = true; rbCollection.Enabled = false; return; } break; case JobContext.Collection: if (!rbCollection.Checked) { rbCollection.Enabled = true; rbCollection.Checked = true; rbServer.Enabled = false; return; } break; case JobContext.Any: rbCollection.Enabled = true; rbServer.Enabled = true; break; default: break; } } TeamFoundationJobService jobService = GetJobService(); jobDefinition = jobService.QueryJobDefinition(currentContext, new Guid(jobId)); if (jobDefinition != null && jobDefinition.Schedule.Count > 0) { UpdateJobDataInUI(); } else { ClearJobDataInUI(job, jobType); } txtJobData.Enabled = !string.IsNullOrEmpty(txtJobData.Text); UpdateRegistryInfo(job.Item1, jobType.GetCustomAttribute <RegistryInfoResourceAttribute>()); tpSchedule.Enabled = true; }
private void btnDeleteSchedule_Click(object sender, EventArgs e) { if (jobDefinition != null) { var jobId = jobDefinition.JobId.ToString(); TeamFoundationJobService jobService = GetJobService(); jobService.UpdateJobDefinitions(currentContext, new[] { jobDefinition.JobId }, null); jobDefinition = null; ReadJobSchedule(jobId); MessageBox.Show("Job definition deleted successfully."); } }
/// <summary> /// Try to find a job definition with a specified name /// </summary> /// <param name="jobName">Job name.</param> /// <param name="jobDefinition">Job definition found, NULL if none found.</param> /// <returns>True if a job was found with the name jobName otherwise False.</returns> private bool TryGetJobByName(string jobName, out TeamFoundationJobDefinition jobDefinition) { jobDefinition = null; var jobDefinitions = CollectionJobService.QueryJobs(); foreach (var definition in jobDefinitions) { if (String.Equals(definition.Name, jobName, StringComparison.InvariantCultureIgnoreCase)) { jobDefinition = definition; return true; } } return false; }
/// <summary> /// Try to find a job definition with a specified name /// </summary> /// <param name="jobName">Job name.</param> /// <param name="jobDefinition">Job definition found, NULL if none found.</param> /// <returns>True if a job was found with the name jobName otherwise False.</returns> private bool TryGetJobByName(string jobName, out TeamFoundationJobDefinition jobDefinition) { jobDefinition = null; var jobDefinitions = CollectionJobService.QueryJobs(); foreach (var definition in jobDefinitions) { if (String.Equals(definition.Name, jobName, StringComparison.InvariantCultureIgnoreCase)) { jobDefinition = definition; return(true); } } return(false); }
/// <summary> /// Create a new job definition for a Warehouse job. /// </summary> /// <remarks> /// This set up the job with the properties required for it to be considered a Warehouse job. /// /// TeamFoundationJobDefinition is sealed so you cannot derive a strongly typed warehouse job. /// </remarks> /// <param name="jobName">Job name.</param> /// <param name="jobExtensionName">Extension name (assembly.class)</param> /// <returns>New job definition for the Warehouse job.</returns> private TeamFoundationJobDefinition CreateWarehouseJobDefinition(string jobName, string jobExtensionName) { // Add data node identifying this as a Warehouse job. var document = new XmlDocument(); var dataElement = document.CreateElement(JobDataElementName); dataElement.InnerText = WarehouseJobDataElementValue; // Create a job definition var newDefinition = new TeamFoundationJobDefinition(jobName, jobExtensionName, dataElement); // Create a default schedule for the job. var schedule = new TeamFoundationJobSchedule(DateTime.UtcNow, WarehouseJobDefaultProcessingInterval); newDefinition.Schedule.Add(schedule); return(newDefinition); }
protected override void DoWork(TeamFoundationRequestContext requestContext, TeamFoundationJobDefinition jobDefinition, DateTime queueTime, ref TeamFoundationJobExecutionResult result, ref string resultMessage, ref bool skipFurtherResultAnalysis) { ReadJobSettings(requestContext); if (!emailEnabled) { resultMessage = string.Format("Email settings have not been configured or are disabled for host {0}", requestContext.ServiceHost.InstanceId); skipFurtherResultAnalysis = true; return; } if (!File.Exists(emailTemplate)) { LogError(string.Format("The template file '{0}' does not exist!", emailTemplate), null); return; } // First delete all outdated workspaces so that they don't get picked up by the warning logic DeleteWorkspaces(requestContext); SendEmail(requestContext); }
public TeamFoundationJobExecutionResult Run( TeamFoundationRequestContext requestContext, TeamFoundationJobDefinition jobDefinition, DateTime queueTime, out string resultMessage) { resultMessage = string.Empty; try { string strConfigFile = this.workflowRunner.ExecutionPath.FullName + ".config"; XmlConfigurator.Configure(new Uri(strConfigFile)); AppendSuffixToLoggingPath("_JobAgent"); this.LogInfo(string.Format("Enter Job for WorkitemChangedEvent")); lock (JobLock) { XmlNode xmlData = jobDefinition.Data; WorkItemChangedEvent workItemChangedEvent = WorkItemChangedEventSerializer.DeserializeXml(xmlData); this.workflowRunner.ProcessEvent(requestContext, workItemChangedEvent); } } catch (RequestCanceledException e) { this.LogError(string.Format("Workflow cancel: "), e); return(TeamFoundationJobExecutionResult.Stopped); } catch (Exception e) { this.LogError(string.Format("Workflow error: "), e); resultMessage = e.ToString(); return(TeamFoundationJobExecutionResult.Failed); } this.LogInfo(string.Format("Leave Job for WorkitemChangedEvent")); return(TeamFoundationJobExecutionResult.Succeeded); }
/// <summary> /// This method is called, when your job is run by the Team Foundation Background Job Agent. /// </summary> /// <param name="requestContext"> /// The request context of the current job run. Use this to get to the Team Foundation Services as necessary. /// </param> /// <param name="jobDefinition"> /// The definition of the current job, which contains scheduling information as well as the job data. /// </param> /// <param name="queueTime">The time this job was scheduled to run.</param> /// <param name="resultMessage"> /// Any result message that should be logged to the job history. Use the <see cref="o:LogError"/> and <see cref="o:LogWarning"/> /// methods to create standard error and warning messages or log exceptions. At the end of your Run method, assign the contents /// of the <see cref="errorsWarningsBuilder"/> to the resultMessage parameter. /// </param> /// <returns></returns> public TeamFoundationJobExecutionResult Run(TeamFoundationRequestContext requestContext, TeamFoundationJobDefinition jobDefinition, DateTime queueTime, out string resultMessage) { TeamFoundationJobExecutionResult result = TeamFoundationJobExecutionResult.Succeeded; resultMessage = string.Empty; try { InitializeLogging(requestContext); bool skipFurtherResultAnalysis = false; DoWork(requestContext, jobDefinition, queueTime, ref result, ref resultMessage, ref skipFurtherResultAnalysis); if (!skipFurtherResultAnalysis) { AnalyzeResults(ref resultMessage, out result); } } catch (Exception ex) { HandleException(ex, out resultMessage, out result); } return(result); }
/// <summary> /// The actual work of the job is being implemented here. /// </summary> /// <param name="requestContext"></param> /// <param name="jobDefinition"></param> /// <param name="queueTime"></param> /// <param name="result"></param> /// <param name="resultMessage"></param> /// <param name="skipFurtherResultAnalysis"></param> protected abstract void DoWork(TeamFoundationRequestContext requestContext, TeamFoundationJobDefinition jobDefinition, DateTime queueTime, ref TeamFoundationJobExecutionResult result, ref string resultMessage, ref bool skipFurtherResultAnalysis);
/// <summary> /// Create a new job definition for a Warehouse job. /// </summary> /// <remarks> /// This set up the job with the properties required for it to be considered a Warehouse job. /// /// TeamFoundationJobDefinition is sealed so you cannot derive a strongly typed warehouse job. /// </remarks> /// <param name="jobName">Job name.</param> /// <param name="jobExtensionName">Extension name (assembly.class)</param> /// <returns>New job definition for the Warehouse job.</returns> private TeamFoundationJobDefinition CreateWarehouseJobDefinition(string jobName, string jobExtensionName) { // Add data node identifying this as a Warehouse job. var document = new XmlDocument(); var dataElement = document.CreateElement(JobDataElementName); dataElement.InnerText = WarehouseJobDataElementValue; // Create a job definition var newDefinition = new TeamFoundationJobDefinition(jobName, jobExtensionName, dataElement); // Create a default schedule for the job. var schedule = new TeamFoundationJobSchedule(DateTime.UtcNow, WarehouseJobDefaultProcessingInterval); newDefinition.Schedule.Add(schedule); return newDefinition; }
public TeamFoundationJobExecutionResult Run(TeamFoundationRequestContext requestContext, TeamFoundationJobDefinition jobDefinition, DateTime queueTime, out string resultMessage) { resultMessage = ""; try { var service = requestContext.GetService <TeamFoundationIdentityService>(); var identities = service.ReadFilteredIdentities(requestContext, "Microsoft.TeamFoundation.Identity.DisplayName CONTAINS '' AND Microsoft.TeamFoundation.Identity.Type == 'User'", 5000, null, true, MembershipQuery.None); if (identities != null) { foreach (var identity in identities.Items) { UpdateImageFromAD(requestContext, identity, service); } } } catch (Exception e) { resultMessage = e.Message + e.StackTrace; return(TeamFoundationJobExecutionResult.Failed); } return(TeamFoundationJobExecutionResult.Succeeded); }
private void btnCreateSchedule_Click(object sender, EventArgs e) { TeamFoundationJobService jobService = GetJobService(); if (jobDefinition == null) { // Create new job definition var jobId = (string)lvJobs.SelectedItems[0].Tag; var jobType = jobs[jobId.ToUpper()].Item2; var jobName = jobType.IsDefined(typeof(JobNameAttribute)) ? jobType.GetCustomAttribute <JobNameAttribute>().JobName : jobType.Name; XmlElement jobData = null; if (!string.IsNullOrEmpty(txtJobData.Text)) { var xElem = XElement.Parse(txtJobData.Text); jobData = new XmlDocument().ReadNode(xElem.CreateReader()) as XmlElement; } var timeZone = (from tzi in TimeZoneInfo.GetSystemTimeZones() where tzi.DisplayName == (string)cbTimeZone.SelectedItem select tzi).FirstOrDefault() ?? TimeZoneInfo.Utc; var schedule = new TeamFoundationJobSchedule(); schedule.TimeZoneId = timeZone.Id; schedule.Interval = (int)nudInterval.Value; schedule.ScheduledTime = dtpScheduledTime.Value; var definition = new TeamFoundationJobDefinition(new Guid(jobId), jobName, jobType.FullName, jobData, Microsoft.TeamFoundation.Framework.Common.TeamFoundationJobEnabledState.Enabled); definition.Schedule.Add(schedule); try { jobService.UpdateJobDefinitions(currentContext, null, new[] { definition }); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error creating job definition", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } ReadJobSchedule(jobId); MessageBox.Show("Job definition created successfully. Please make sure to copy the necessary assemblies to the TFS job agent's plugin folder and restart the job agent!"); } else { // Update existing job definition var timeZone = (from tzi in TimeZoneInfo.GetSystemTimeZones() where tzi.DisplayName == (string)cbTimeZone.SelectedItem select tzi).FirstOrDefault() ?? TimeZoneInfo.Utc; jobDefinition.Schedule[0].ScheduledTime = dtpScheduledTime.Value; jobDefinition.Schedule[0].TimeZoneId = timeZone.Id; jobDefinition.Schedule[0].Interval = (int)nudInterval.Value; XmlElement jobData = null; if (!string.IsNullOrEmpty(txtJobData.Text)) { var xElem = XElement.Parse(txtJobData.Text); jobData = new XmlDocument().ReadNode(xElem.CreateReader()) as XmlElement; } jobDefinition.Data = jobData; try { jobService.UpdateJobDefinitions(currentContext, null, new[] { jobDefinition }); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error updating job definition", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } ReadJobSchedule(jobDefinition.JobId.ToString()); MessageBox.Show("Job definition updated successfully."); } }