Esempio n. 1
0
        private async Task DoActivity(ActivityFlowStep activityFlowStep, FlowContext stepFlowContext,
                                      FlowValues flowValues, FlowTrace flowTrace, CancellationToken cancellationToken)
        {
            var activityRequest = CreateRequest(stepFlowContext, activityFlowStep, flowValues);

            if (IsRequestDisabled(activityRequest))
            {
                _logger?.LogActivityRequestDisabled(stepFlowContext, activityRequest);
                return;
            }

            _logger?.LogActivityRequest(stepFlowContext, activityRequest);

            var stopWatch = new Stopwatch();

            stopWatch.Start();

            object activityResponse =
                await GetActivityResponse(
                    stepFlowContext, activityFlowStep, activityRequest, cancellationToken);

            stopWatch.Stop();

            ValidateFlowObjectValues(activityResponse, activityResponse.GetType().GetFlowObjectType(), activityFlowStep.Name);

            flowTrace.AddStep(new FlowTraceStep {
                StepType = FlowTraceStepType.Activity, Name = activityFlowStep.Name
            });

            _logger?.LogActivityResponse(stepFlowContext, activityResponse, stopWatch.ElapsedMilliseconds);

            SetFlowValuesFromResponse(activityFlowStep.Definition, stepFlowContext, flowValues, activityRequest, activityResponse);

            AppendResponseFlowTrace(flowTrace, activityResponse);
        }
Esempio n. 2
0
        private void RecordLabelStep(FlowTrace flowTrace, FlowContext stepFlowContext)
        {
            _logger.LogLabel(stepFlowContext);

            flowTrace.AddStep(new FlowTraceStep {
                StepType = FlowTraceStepType.Label, Name = stepFlowContext.FlowStepName
            });
        }
Esempio n. 3
0
        public async Task <TFlowResponse> Handle(TFlowRequest flowRequest, CancellationToken cancellationToken)
        {
            // TODO: Respect the cancellation token

            var flowContext = new FlowContext(typeof(TFlowRequest), flowRequest.FlowContext);

            try
            {
                var stopWatch = new Stopwatch();
                stopWatch.Start();

                using (flowContext.IsRootFlow ? _logger?.BeginFlowScope(flowContext) : null)
                {
                    _logger?.LogFlowRequest(flowContext, flowRequest);

                    var flowDefinition = ResolveFlowDefinition(flowRequest, flowContext);

                    var flowValues = GetInitialFlowValues(flowDefinition, flowRequest);

                    var flowTrace = new FlowTrace();

                    var flowStepIndex = 0;

                    while (flowStepIndex < flowDefinition.Steps.Count)
                    {
                        var flowStep        = flowDefinition.Steps[flowStepIndex];
                        var stepFlowContext = flowContext.GetStepContext(flowStep.Name);

                        OnDebugEvent(flowStep.Name, FlowDebugEvent.PreStep, flowValues);

                        flowStepIndex =
                            await PerformFlowStep(
                                stepFlowContext, flowDefinition, flowStep, flowStepIndex, flowValues, flowTrace, cancellationToken);

                        OnDebugEvent(flowStep.Name, FlowDebugEvent.PostStep, flowValues);
                    }

                    var flowResponse = BuildFlowResponse(flowContext, flowDefinition, flowTrace, flowValues);

                    stopWatch.Stop();

                    _logger?.LogFlowResponse(flowContext, flowResponse, stopWatch.ElapsedMilliseconds);

                    return(flowResponse);
                }
            }
            catch (Exception ex) when(LogFlowError(flowContext, flowRequest, ex))
            {
                throw;
            }
        }
Esempio n. 4
0
        private static TFlowResponse BuildFlowResponse(FlowContext flowContext, FlowDefinition <TFlowRequest, TFlowResponse> flowDefinition,
                                                       FlowTrace flowTrace, FlowValues flowValues)
        {
            var flowResponseType = typeof(TFlowResponse);
            var flowResponse     = (TFlowResponse)Activator.CreateInstance(flowResponseType);
            var flowObjectType   = flowResponseType.GetFlowObjectType();

            var missingMandatoryPropertyNames = new List <string>();

            foreach (var flowObjectProperty in flowObjectType.Properties)
            {
                var binding =
                    flowDefinition.Finalizer?.Inputs.Find(b => b.Property.Name == flowObjectProperty.Name)
                    ?? new FlowValueInputBinding(flowObjectProperty)
                {
                    FlowValueSelector = new FlowValueSingleSelector(flowObjectProperty.Name)
                };

                if (binding.TryGetRequestValue(flowValues, flowResponse, out var responseValue))
                {
                    flowObjectProperty.PropertyInfo.SetValue(flowResponse, responseValue);
                }

                CheckMandatoryFlowObjectProperty(flowResponse, flowObjectProperty, missingMandatoryPropertyNames);
            }

            if (missingMandatoryPropertyNames.Count > 0)
            {
                throw new FlowException(
                          $"The following mandatory properties were not populated on {flowResponseType.FullName}: " +
                          $"{string.Join(", ", missingMandatoryPropertyNames.ToArray())}");
            }

            if (flowResponse is FlowResponse baseFlowResponse)
            {
                baseFlowResponse.CorrelationId  = flowContext.CorrelationId;
                baseFlowResponse.RequestId      = flowContext.RequestId;
                baseFlowResponse.FlowInstanceId = flowContext.FlowInstanceId;
                baseFlowResponse.Trace          = flowTrace;
            }

            return(flowResponse);
        }
Esempio n. 5
0
        private static void AppendResponseFlowTrace(FlowTrace flowTrace, dynamic activityResponse)
        {
            var subFlowTrace = activityResponse is FlowResponse flowStepResponse ? flowStepResponse.Trace : null;

            flowTrace.AddSubFlowTrace(subFlowTrace);
        }
Esempio n. 6
0
        private int CheckDecision(int flowStepIndex, DecisionFlowStepBase decisionFlowStep, FlowDefinition <TFlowRequest, TFlowResponse> flowDefinition,
                                  FlowContext stepFlowContext, FlowValues flowValues, FlowTrace flowTrace)
        {
            var decisionRequest = (FlowDecisionBase)CreateRequest(stepFlowContext, decisionFlowStep, flowValues);

            // TODO: Pass the targets directly into GetMatchingBranchIndex
            decisionFlowStep.Branches.ForEach(b => decisionRequest.AddBranch(b.Targets));

            _logger?.LogDecisionRequest(stepFlowContext, decisionRequest);

            var branchIndex = decisionRequest.GetMatchingBranchIndex();

            if (branchIndex < 0 || branchIndex >= decisionFlowStep.Branches.Count)
            {
                throw new FlowException($"The branch index returned was out of bounds of the branch array: {branchIndex}");
            }

            var branch = decisionFlowStep.Branches[branchIndex];

            flowTrace.AddStep(new FlowTraceStep
            {
                StepType = FlowTraceStepType.Decision, Name = decisionFlowStep.Name, BranchTargets = branch.Targets
            });

            _logger?.LogDecisionResponse(stepFlowContext, branch);

            if (branch.IsEnd)
            {
                return(int.MaxValue);
            }

            if (branch.IsUnhandled)
            {
                throw new FlowUnhandledElseException($"Unhandled ELSE for decision '{decisionFlowStep.Name}'");
            }

            var isContinue = (branch.NextStepName == null);

            if (isContinue)
            {
                return(flowStepIndex + 1);
            }

            var nextFlowStepIndex = flowDefinition.GetStepIndex(branch.NextStepName);

            return(nextFlowStepIndex);
        }
Esempio n. 7
0
        private async Task <int> PerformFlowStep(FlowContext stepFlowContext, FlowDefinition <TFlowRequest, TFlowResponse> flowDefinition, FlowStep flowStep,
                                                 int flowStepIndex, FlowValues flowValues, FlowTrace flowTrace, CancellationToken cancellationToken)
        {
            int nextFlowStepIndex;

            switch (flowStep)
            {
            case ActivityFlowStep activityFlowStep:
                await DoActivity(
                    activityFlowStep, stepFlowContext, flowValues,
                    flowTrace, cancellationToken);

                nextFlowStepIndex = flowStepIndex + 1;
                break;

            case DecisionFlowStepBase decisionFlowStep:
                nextFlowStepIndex =
                    CheckDecision(
                        flowStepIndex, decisionFlowStep, flowDefinition, stepFlowContext, flowValues,
                        flowTrace);
                break;

            case GotoFlowStep gotoFlowStep:
                _logger.LogGoto(stepFlowContext, gotoFlowStep.NextStepName);
                nextFlowStepIndex = flowDefinition.GetStepIndex(gotoFlowStep.NextStepName);
                break;

            case LabelFlowStep _:
                RecordLabelStep(flowTrace, stepFlowContext);
                nextFlowStepIndex = flowStepIndex + 1;
                break;

            case EndFlowStep _:
                nextFlowStepIndex = int.MaxValue;
                break;

            default:
                throw new FlowException(
                          $"Unexpected flow activityFlowStep type {flowStep.GetType().FullName}");
            }

            return(nextFlowStepIndex);
        }
Esempio n. 8
0
 internal void AddSubFlowTrace(FlowTrace subFlowTrace)
 {
     _steps.Last().SubFlowTrace = subFlowTrace;
 }