private void zExecuteStep()
        {
            zOnStepBegin(new StepEventArgs(CurrentStep));

            IStepExecutor stepExecutor = m_StepExecutorProvider.GetStepExecutor(CurrentStep);

            stepExecutor.LoadScope(CurrentStep, m_Context);

            StepResult result = stepExecutor.ExecuteStep();

            if (result == StepResult.Failed)
            {
                switch (CurrentStep.FailureScope)
                {
                case StepFailureScope.Step:
                    //Do nothing
                    break;

                case StepFailureScope.Iteration:
                    m_Context.ExecutionStack.SkipIteration();
                    break;

                case StepFailureScope.Group:
                    m_Context.ExecutionStack.BreakBranch();
                    break;

                case StepFailureScope.Sequence:
                    m_ExecutionState = ExecutionState.Fail;
                    break;
                }
            }

            zOnStepComplete(new StepCompleteEventArgs(CurrentStep, result));
        }
Exemple #2
0
        public WorkflowExecutorFixture()
        {
            Host = A.Fake <IWorkflowHost>();
            PersistenceProvider   = A.Fake <IPersistenceProvider>();
            ServiceProvider       = A.Fake <IServiceProvider>();
            ScopeProvider         = A.Fake <IScopeProvider>();
            Registry              = A.Fake <IWorkflowRegistry>();
            ResultProcesser       = A.Fake <IExecutionResultProcessor>();
            EventHub              = A.Fake <ILifeCycleEventPublisher>();
            CancellationProcessor = A.Fake <ICancellationProcessor>();
            DateTimeProvider      = A.Fake <IDateTimeProvider>();
            MiddlewareRunner      = A.Fake <IWorkflowMiddlewareRunner>();
            StepExecutor          = A.Fake <IStepExecutor>();

            Options = new WorkflowOptions(A.Fake <IServiceCollection>());

            var stepExecutionScope = A.Fake <IServiceScope>();

            A.CallTo(() => ScopeProvider.CreateScope(A <IStepExecutionContext> ._)).Returns(stepExecutionScope);
            A.CallTo(() => stepExecutionScope.ServiceProvider).Returns(ServiceProvider);

            var scope        = A.Fake <IServiceScope>();
            var scopeFactory = A.Fake <IServiceScopeFactory>();

            A.CallTo(() => ServiceProvider.GetService(typeof(IServiceScopeFactory))).Returns(scopeFactory);
            A.CallTo(() => scopeFactory.CreateScope()).Returns(scope);
            A.CallTo(() => scope.ServiceProvider).Returns(ServiceProvider);

            A.CallTo(() => DateTimeProvider.Now).Returns(DateTime.Now);
            A.CallTo(() => DateTimeProvider.UtcNow).Returns(DateTime.UtcNow);

            A
            .CallTo(() => ServiceProvider.GetService(typeof(IWorkflowMiddlewareRunner)))
            .Returns(MiddlewareRunner);

            A
            .CallTo(() => ServiceProvider.GetService(typeof(IStepExecutor)))
            .Returns(StepExecutor);

            A.CallTo(() => MiddlewareRunner
                     .RunPostMiddleware(A <WorkflowInstance> ._, A <WorkflowDefinition> ._))
            .Returns(Task.CompletedTask);

            A.CallTo(() => MiddlewareRunner
                     .RunExecuteMiddleware(A <WorkflowInstance> ._, A <WorkflowDefinition> ._))
            .Returns(Task.CompletedTask);

            A.CallTo(() => StepExecutor.ExecuteStep(A <IStepExecutionContext> ._, A <IStepBody> ._))
            .ReturnsLazily(call =>
                           call.Arguments[1].As <IStepBody>().RunAsync(
                               call.Arguments[0].As <IStepExecutionContext>()));

            //config logging
            var loggerFactory = new LoggerFactory();

            //loggerFactory.AddConsole(LogLevel.Debug);

            Subject = new WorkflowExecutor(Registry, ServiceProvider, ScopeProvider, DateTimeProvider, ResultProcesser, EventHub, CancellationProcessor, Options, loggerFactory);
        }
        private async Task ExecuteStep(WorkflowInstance workflow, WorkflowStep step, ExecutionPointer pointer, WorkflowExecutorResult wfResult, WorkflowDefinition def, CancellationToken cancellationToken = default)
        {
            IStepExecutionContext context = new StepExecutionContext()
            {
                Workflow          = workflow,
                Step              = step,
                PersistenceData   = pointer.PersistenceData,
                ExecutionPointer  = pointer,
                Item              = pointer.ContextItem,
                CancellationToken = cancellationToken
            };

            using (var scope = _scopeProvider.CreateScope(context))
            {
                _logger.LogDebug("Starting step {0} on workflow {1}", step.Name, workflow.Id);

                IStepBody body = step.ConstructBody(scope.ServiceProvider);

                if (body == null)
                {
                    _logger.LogError("Unable to construct step body {0}", step.BodyType.ToString());
                    pointer.SleepUntil = _datetimeProvider.UtcNow.Add(_options.ErrorRetryInterval);
                    wfResult.Errors.Add(new ExecutionError()
                    {
                        WorkflowId         = workflow.Id,
                        ExecutionPointerId = pointer.Id,
                        ErrorTime          = _datetimeProvider.UtcNow,
                        Message            = $"Unable to construct step body {step.BodyType.ToString()}"
                    });
                    return;
                }

                foreach (var input in step.Inputs)
                {
                    input.AssignInput(workflow.Data, body, context);
                }

                switch (step.BeforeExecute(wfResult, context, pointer, body))
                {
                case ExecutionPipelineDirective.Defer:
                    return;

                case ExecutionPipelineDirective.EndWorkflow:
                    workflow.Status       = WorkflowStatus.Complete;
                    workflow.CompleteTime = _datetimeProvider.UtcNow;
                    return;
                }

                var result = await _stepExecutor.ExecuteStep(context, body);

                if (result.Proceed)
                {
                    foreach (var output in step.Outputs)
                    {
                        output.AssignOutput(workflow.Data, body, context);
                    }
                }

                _executionResultProcessor.ProcessExecutionResult(workflow, def, pointer, step, result, wfResult);
                step.AfterExecute(wfResult, context, result, pointer);
            }
        }