Example #1
0
        private static async Task InvokeListenerOnAfterExecutionState(StateExecutionContext stateExecutionContext,
                                                                      IStateListener stateListener, StateExecutionResult stateExecutionResult, CancellationToken cancellationToken = default)
        {
            if (null == stateListener)
            {
                return;
            }

            Log.Verbose("State has been processed [{state}] listener [after state] [{workflowInstanceID}]",
                        stateExecutionContext.StateConfiguration.Code, stateExecutionContext.WorkflowContext.WorkflowInstance.Id);

            try
            {
                await stateListener.AfterExecutionState(stateExecutionContext, stateExecutionResult, cancellationToken).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                throw new WorkflowException(string.Format(CultureInfo.InvariantCulture,
                                                          "An error has occurred during execution of an state [{0}] listener [after state] for workflow instance [{1}]",
                                                          stateExecutionContext.StateConfiguration.Code,
                                                          stateExecutionContext.WorkflowContext.WorkflowInstance.Id), ex);
            }
        }
Example #2
0
        public async Task <StateExecutionResult> Process(StateExecutionContext stateExecutionContext, CancellationToken cancellationToken = default)
        {
            // 1. log starting
            // 2. listener --> before processing
            // 3. run activities
            //    3.1. execute all synchronous activities
            //    3.2. TODO: schedule all asynchronous activities
            // 4. listener --> after activities
            // 5. if state is final --> report and exit
            // 6. evaluate non-delayed transitions
            //    6.1. coded transitions
            //    6.2. compiled conditions
            // 7. schedule all delayed transitions
            // 8. log finishing

            IStateListener       stateListener        = null;
            StateExecutionResult stateExecutionResult = null;
            var workflowInstance = stateExecutionContext.WorkflowContext.WorkflowInstance;

            try
            {
                Log.Verbose("Starting processing state [{type}::{state}] [{workflowInstanceId}]", stateExecutionContext.StateConfiguration.Type,
                            stateExecutionContext.StateConfiguration.Code, stateExecutionContext.WorkflowContext.WorkflowInstance.Id);

                workflowInstance.CurrentStateCode     = stateExecutionContext.StateConfiguration.Code;
                workflowInstance.CurrentStateProgress = StateExecutionProgress.Started;
                await _workflowEngine.SaveWorkflowInstance(workflowInstance, cancellationToken).ConfigureAwait(false);

                stateListener = GetStateListener(stateExecutionContext.WorkflowContext);
                workflowInstance.CurrentStateProgress = StateExecutionProgress.BeforeActivities;
                await InvokeListenerOnBeforeExecutingState(stateExecutionContext, stateListener, cancellationToken).ConfigureAwait(false);

                var activityExecutionResult = await ExecuteSynchronousActivities(stateExecutionContext, cancellationToken).ConfigureAwait(false);

                if (activityExecutionResult.Status.IsFailed())
                {
                    workflowInstance.CurrentStateProgress = StateExecutionProgress.Completed;

                    if (!string.IsNullOrEmpty(activityExecutionResult.TransitionToUse))
                    {
                        var transitionConfiguration = stateExecutionContext.StateConfiguration.Transitions.FirstOrDefault(tc =>
                                                                                                                          string.Equals(tc.MoveToState, activityExecutionResult.TransitionToUse, StringComparison.OrdinalIgnoreCase));

                        if (null == transitionConfiguration)
                        {
                            throw new WorkflowException(string.Format(CultureInfo.InvariantCulture,
                                                                      "Cannot not find transition [{0}] for failed activity in status [{1}], [workflow ID={2:D}]",
                                                                      activityExecutionResult.TransitionToUse,
                                                                      stateExecutionContext.StateConfiguration.Code,
                                                                      stateExecutionContext.WorkflowContext.WorkflowInstance.Id));
                        }

                        Log.Verbose("[{type}::{state}] has been failed, transitioning to custom {failureState} [{workflowInstanceId}]",
                                    stateExecutionContext.StateConfiguration.Type, stateExecutionContext.StateConfiguration.Code,
                                    transitionConfiguration.MoveToState, stateExecutionContext.WorkflowContext.WorkflowInstance.Id);

                        // stateExecutionResult is being used later in the final block
                        stateExecutionResult = new StateExecutionResult(StateExecutionStatus.Failed, new StateExecutionTransition(StateTypeConfiguration.Application, transitionConfiguration));
                        return(stateExecutionResult);
                    }

                    Log.Verbose("[{type}::{state}] has been failed [{workflowInstanceId}]", stateExecutionContext.StateConfiguration.Type,
                                stateExecutionContext.StateConfiguration.Code, stateExecutionContext.WorkflowContext.WorkflowInstance.Id);

                    // stateExecutionResult is being used later in the final block
                    stateExecutionResult = new StateExecutionResult(StateExecutionStatus.Failed, new StateExecutionTransition(StateTypeConfiguration.Failed, null));
                    return(stateExecutionResult);
                }

                workflowInstance.CurrentStateProgress = StateExecutionProgress.AfterActivitiesProcessed;
                await _workflowEngine.SaveWorkflowInstance(workflowInstance, cancellationToken).ConfigureAwait(false);
                await InvokeListenerOnAfterExecutionActivities(stateExecutionContext, stateListener, cancellationToken).ConfigureAwait(false);

                if (stateExecutionContext.StateConfiguration.Type == StateTypeConfiguration.Failed ||
                    stateExecutionContext.StateConfiguration.Type == StateTypeConfiguration.Final)
                {
                    Log.Verbose("[{type}::{state}] has been completed [{workflowInstanceId}]", stateExecutionContext.StateConfiguration.Type,
                                stateExecutionContext.StateConfiguration.Code, stateExecutionContext.WorkflowContext.WorkflowInstance.Id);

                    workflowInstance.CurrentStateProgress = StateExecutionProgress.Completed;
                    await _workflowEngine.SaveWorkflowInstance(workflowInstance, cancellationToken).ConfigureAwait(false);

                    // stateExecutionResult is being used later in the final block
                    stateExecutionResult = new StateExecutionResult(StateExecutionStatus.Finished, new StateExecutionTransition(StateTypeConfiguration.Undefined, null));
                    return(stateExecutionResult);
                }

                workflowInstance.CurrentStateProgress = StateExecutionProgress.BeforeTransitions;
                await _workflowEngine.SaveWorkflowInstance(workflowInstance, cancellationToken).ConfigureAwait(false);

                var stateExecutionTransition = await EvaluateNonDelayTransitions(stateExecutionContext, cancellationToken).ConfigureAwait(false);

                var delayedStateExecutionTransitions = GetDelayedTransitions(stateExecutionContext);

                if (stateExecutionTransition.NextStateType == StateTypeConfiguration.Undefined && null == delayedStateExecutionTransitions)
                {
                    throw new WorkflowException(string.Format(CultureInfo.InvariantCulture,
                                                              "Cannot evaluate transition(s) for non final state [{0}], [workflow ID={1:D}]",
                                                              stateExecutionContext.StateConfiguration.Code,
                                                              stateExecutionContext.WorkflowContext.WorkflowInstance.Id));
                }

                if (null != delayedStateExecutionTransitions)
                {
                    await ScheduleDelayedTransitions(stateExecutionContext, delayedStateExecutionTransitions, cancellationToken)
                    .ConfigureAwait(false);
                }

                workflowInstance.CurrentStateProgress = StateExecutionProgress.Completed;
                await _workflowEngine.SaveWorkflowInstance(workflowInstance, cancellationToken).ConfigureAwait(false);

                stateExecutionResult = new StateExecutionResult(StateExecutionStatus.Completed, stateExecutionTransition);
                return(stateExecutionResult);
            }
            catch (Exception ex)
            {
                Log.Error(ex, "An error has occurred during processing state [{type}::{state}] [{workflowInstanceId}]",
                          stateExecutionContext.StateConfiguration.Type, stateExecutionContext.StateConfiguration.Code, stateExecutionContext.WorkflowContext.WorkflowInstance.Id);

                workflowInstance.CurrentStateProgress = StateExecutionProgress.Completed;
                if (stateExecutionContext.StateConfiguration.Type == StateTypeConfiguration.Failed)
                {
                    stateExecutionResult = new StateExecutionResult(StateExecutionStatus.Failed, new StateExecutionTransition(StateTypeConfiguration.Undefined, null));
                    await _workflowEngine.SaveWorkflowInstance(workflowInstance, cancellationToken).ConfigureAwait(false);

                    return(stateExecutionResult);
                }

                stateExecutionResult = new StateExecutionResult(StateExecutionStatus.Failed, new StateExecutionTransition(StateTypeConfiguration.Failed, null));
                await _workflowEngine.SaveWorkflowInstance(workflowInstance, cancellationToken).ConfigureAwait(false);

                return(stateExecutionResult);
            }
            finally
            {
                await InvokeListenerOnAfterExecutionState(stateExecutionContext, stateListener, stateExecutionResult, cancellationToken).ConfigureAwait(false);

                Log.Debug("Finished processing state [{type}::{code}], {status}, {nextState} [{workflowInstanceId}]",
                          stateExecutionContext.StateConfiguration.Type, stateExecutionContext.StateConfiguration.Code,
                          stateExecutionResult?.Status, stateExecutionResult?.Transition.TransitionConfiguration?.Code, stateExecutionContext.WorkflowContext.WorkflowInstance.Id);
            }
        }