public async Task ReportJobAssigned(string message, CancellationToken cancellationToken) { var startedEvent = new JobAssignedEvent(this.taskMessage.JobId); var taskClient = GetTaskClient(this.taskMessage.PlanUri, this.taskMessage.AuthToken); await taskClient.RaisePlanEventAsync(this.taskMessage.ProjectId, this.taskMessage.HubName, this.taskMessage.PlanId, startedEvent, cancellationToken).ConfigureAwait(false); this.taskLogger.Log($"Job started: {message}"); }
private async Task ProcessMessage(IServiceBusMessage message, IVstsScheduleHandler <T> handler, CancellationToken cancellationToken, T vstsMessage, IDictionary <string, string> eventProperties) { // create client var projectId = vstsMessage.ProjectId; var planId = vstsMessage.PlanId; var vstsPlanUrl = vstsMessage.VstsPlanUri; var vstsUrl = vstsMessage.VstsUri; var authToken = vstsMessage.AuthToken; var parentTimelineId = vstsMessage.TimelineId; var jobId = vstsMessage.JobId; var hubName = vstsMessage.VstsHub.ToString(); var taskHttpClient = this.GetTaskClient(vstsPlanUrl, authToken, vstsMessage.SkipRaisePlanEvents); // create a timeline if required var timelineName = string.Format("{0}_{1}", this.settings.TimeLineNamePrefix, jobId.ToString("D")); var taskLogId = await this.GetOrCreateTaskLogId(message, cancellationToken, taskHttpClient, projectId, planId, jobId, parentTimelineId, timelineName, hubName).ConfigureAwait(false); eventProperties[VstsMessageConstants.TaskLogIdPropertyName] = taskLogId.ToString(); vstsMessage.TaskLogId = taskLogId; // setup VSTS instrumentation and wrap handler var vstsLogger = new VstsLogger(clientLogger, taskHttpClient, hubName, projectId, planId, taskLogId, parentTimelineId, jobId); var loggersAggregate = new LoggersAggregate(new List <ILogger> { clientLogger, vstsLogger }); var instrumentedHandler = new HandlerWithInstrumentation <T>(loggersAggregate, handler); // process request if (vstsMessage.RequestType == RequestType.Cancel) { // attempt to cancel await instrumentedHandler.Cancel(vstsMessage, cancellationToken).ConfigureAwait(false); } else { // already cancelled? var buildHttpClientWrapper = GetBuildClient(vstsUrl, authToken); var releaseHttpClientWrapper = GetReleaseClient(vstsPlanUrl, authToken); var isSessionValid = await JobStatusReportingHelper.IsSessionValid(vstsMessage, buildHttpClientWrapper, releaseHttpClientWrapper, cancellationToken).ConfigureAwait(false); if (!isSessionValid) { await clientLogger.LogInfo("SessionAlreadyCancelled", string.Format("Skipping Execute for cancelled or deleted {0}", vstsMessage.VstsHub), eventProperties, cancellationToken).ConfigureAwait(false); return; } // raise assigned event (to signal we got the message) var assignedEvent = new JobAssignedEvent(jobId); await taskHttpClient.RaisePlanEventAsync(projectId, hubName, planId, assignedEvent, cancellationToken).ConfigureAwait(false); // attempt to schedule var scheduleResult = await instrumentedHandler.Execute(vstsMessage, cancellationToken).ConfigureAwait(false); var reportingHelper = GetVstsJobStatusReportingHelper(vstsMessage, vstsLogger); if (scheduleResult.ScheduleFailed) { // must first call job started, otherwise it cannot be completed await reportingHelper.ReportJobStarted(DateTimeOffset.Now, "Started processing job.", CancellationToken.None).ConfigureAwait(false); await reportingHelper.ReportJobCompleted(DateTimeOffset.Now, string.Format("Failed to schedule job. Message: {0}", scheduleResult.Message), false, CancellationToken.None).ConfigureAwait(false); } else if (vstsMessage.CompleteSychronously) { // raise completed event await reportingHelper.ReportJobCompleted(DateTimeOffset.Now, "Completed processing job.", true, CancellationToken.None).ConfigureAwait(false); } } }