public async Task <TaskResult> Execute(CancellationToken cancellationToken) { using (var taskClient = new TaskClient(taskProperties)) { var taskResult = TaskResult.Failed; try { // create timelinerecord if not provided await CreateTaskTimelineRecordIfRequired(taskClient, cancellationToken).ConfigureAwait(false); taskLogger = new TaskLogger(taskProperties, taskClient); // report task started await taskLogger.Log("Task started").ConfigureAwait(false); await taskClient.ReportTaskStarted(taskProperties.TaskInstanceId, cancellationToken).ConfigureAwait(false); await taskClient.ReportTaskProgress(taskProperties.TaskInstanceId, cancellationToken).ConfigureAwait(false); // start client handler execute var executeTask = taskExecutionHandler.ExecuteAsync(taskMessage, taskLogger, cancellationToken).ConfigureAwait(false); taskResult = await executeTask; // report task completed with status await taskLogger.Log("Task completed").ConfigureAwait(false); await taskClient.ReportTaskCompleted(taskProperties.TaskInstanceId, taskResult, cancellationToken).ConfigureAwait(false); return(taskResult); } catch (Exception e) { if (taskLogger != null) { await taskLogger.Log(e.ToString()).ConfigureAwait(false); } await taskClient.ReportTaskCompleted(taskProperties.TaskInstanceId, taskResult, cancellationToken).ConfigureAwait(false); throw; } finally { if (taskLogger != null) { await taskLogger.End().ConfigureAwait(false); } } } }
public async Task Cancel(CancellationToken cancellationToken) { taskLogger.Log("Canceling task ..."); using (var taskClient = new TaskClient(taskProperties)) { taskLogger = new TaskLogger(taskProperties, taskClient); this.taskExecutionHandler.CancelAsync(taskMessage, taskLogger, cancellationToken); await taskClient.ReportTaskCompleted(taskProperties.TaskInstanceId, TaskResult.Canceled, cancellationToken).ConfigureAwait(false); } }