public TaskResult Execute( TaskDefinition task, IProgress <TaskProgress> progress = null, CancellationToken?cancellation = null) { if (progress == null) { progress = new EmptyProgress <TaskProgress>(); } if (cancellation == null) { cancellation = CancellationToken.None; } var currentOutcome = TaskOutcome.Successful; var errors = new List <TaskExecutionException>(); var events = _executionEventsBag.TryGetEvents <TaskExecutionEvents>(); var taskSw = new Stopwatch(); var stepSw = new Stopwatch(); events?.OnTaskStarted(task); taskSw.Start(); foreach (var step in task.Steps) { if (!step.ExecutionCondition(currentOutcome)) { events?.OnStepSkipped(step, task); continue; } events?.OnStepStarted(step, task); stepSw.Restart(); try { ExecuteStepMethod.InvokeAsGeneric( this, new[] { step.GetType() }, step, task, progress, cancellation); } catch (OperationCanceledException e) { if (currentOutcome < TaskOutcome.Canceled) { currentOutcome = TaskOutcome.Canceled; } events?.OnStepCanceled(step, task); } catch (TaskExecutionException e) { errors.Add(e); if (currentOutcome < TaskOutcome.Failed) { currentOutcome = TaskOutcome.Failed; } events?.OnStepFailed(e, step, task); } stepSw.Stop(); events?.OnStepEnded(step, task, stepSw.Elapsed); } var result = new TaskResult( currentOutcome == TaskOutcome.Canceled, errors); taskSw.Stop(); events?.OnTaskEnded(task, result, taskSw.Elapsed); return(result); }
public TaskResult Execute( TaskDefinition task, CancellationToken?cancellation = null, params IExecutionEvents[] events) { if (cancellation == null) { cancellation = CancellationToken.None; } var eventsBag = new ExecutionEventsBag(_globalEventsBag, new ExecutionEventsBag(events)); var outcomeSoFar = TaskOutcome.Successful; var errors = new List <TaskExecutionException>(); var taskEvents = eventsBag.TryGetEvents <TaskExecutionEvents>(); var taskSw = new Stopwatch(); var stepSw = new Stopwatch(); taskEvents?.Raise(x => x.OnTaskStarted(task)); taskSw.Start(); foreach (var step in task.Steps) { if (!step.ExecutionCondition(outcomeSoFar)) { taskEvents?.Raise(x => x.OnStepSkipped(step, task)); continue; } taskEvents?.Raise(x => x.OnStepStarted(step, task)); stepSw.Restart(); try { ExecuteStepMethod.InvokeAsGeneric( this, new[] { step.GetType() }, step, task, outcomeSoFar, cancellation, eventsBag); } catch (OperationCanceledException e) { if (outcomeSoFar < TaskOutcome.Canceled) { outcomeSoFar = TaskOutcome.Canceled; } taskEvents?.Raise(x => x.OnStepCanceled(step, task)); } catch (TaskExecutionException e) { errors.Add(e); if (outcomeSoFar < TaskOutcome.Failed) { outcomeSoFar = TaskOutcome.Failed; } taskEvents?.Raise(x => x.OnStepFailed(e, step, task)); } stepSw.Stop(); taskEvents?.Raise(x => x.OnStepEnded(step, task, stepSw.Elapsed)); } var result = new TaskResult( outcomeSoFar == TaskOutcome.Canceled, errors); taskSw.Stop(); taskEvents?.Raise(x => x.OnTaskEnded(task, result, taskSw.Elapsed)); return(result); }