예제 #1
0
 public TaskExecutor(
     ITaskStepExecutorResolver taskStepExecutorResolver,
     ExecutionEventsBag executionEventsBag)
 {
     _taskStepExecutorResolver = taskStepExecutorResolver;
     _executionEventsBag       = executionEventsBag;
 }
예제 #2
0
 public TaskExecutor(
     ITaskStepExecutorResolver taskStepExecutorResolver,
     ExecutionEventsBag globalEventsBag)
 {
     _taskStepExecutorResolver = taskStepExecutorResolver;
     _globalEventsBag          = globalEventsBag;
 }
예제 #3
0
 public ExecutionEventsBag(
     ExecutionEventsBag other1,
     ExecutionEventsBag other2)
 {
     _events = other1._events.Merge(
         other2._events,
         (x, y) => x.Merge(y));
 }
예제 #4
0
        private void ExecuteStep <TStep>(
            TStep step,
            TaskDefinition task,
            TaskOutcome outcomeSoFar,
            CancellationToken cancellation,
            ExecutionEventsBag eventsBag)
            where TStep : ITaskStep
        {
            var stepExecutor = _taskStepExecutorResolver.Resolve <TStep>();

            var context = new TaskStepExecutionContext(
                task,
                eventsBag,
                outcomeSoFar);

            stepExecutor.Execute(step, context, cancellation);
        }
예제 #5
0
        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);
        }