Example #1
0
        protected override object GetPipelineObject(IStepConfiguration @object)
        {
            if (@object.Instance is Type type)
            {
                return(_serviceProvider.GetRequiredService(type));
            }

            return(base.GetPipelineObject(@object));
        }
Example #2
0
        public async Task <bool> Run(CancellationToken cancellationToken = default(CancellationToken))
        {
            try
            {
                InitGlobalVariable();

                _pipelineConfiguration.BeforeStart?.Invoke(_pipelineContext);

                var      steps        = _pipelineConfiguration.GetSteps().ToArray();
                string[] executedStep = new string[steps.Count()];

                bool error = false;

                List <Exception> exceptions = new List <Exception>();

                for (int i = 0; i < steps.Length; i++)
                {
                    IStepConfiguration stepConfiguration = steps[i];

                    try
                    {
                        _pipelineContext.CurrentStepId = stepConfiguration.Id;

                        if (cancellationToken.IsCancellationRequested && (error == false || stepConfiguration.AlwaysRun))
                        {
                            continue;
                        }

                        _pipelineContext.ClearLocalVariable();
                        _pipelineContext.SetLocalVariable(stepConfiguration.LocalVariable);
                        _pipelineContext.SetCurrentScope(stepConfiguration.Scope);

                        _pipelineConfiguration.BeforeStep?.Invoke(_pipelineContext);

                        var pip = GetPipelineObject(stepConfiguration);

                        if (pip is IPipeline pipeline)
                        {
                            await pipeline.Execute(_pipelineContext, cancellationToken);
                        }
                        else
                        {
                            var type             = pip.GetType();
                            var genericInterface = type.GetInterface(typeof(IPipeline <>).Name);

                            if (genericInterface != null)
                            {
                                var command = stepConfiguration.GetType().GetProperty("Command").GetValue(stepConfiguration);

                                var  method = type.GetMethod("Execute");
                                Task task   = (Task)method.Invoke(pip, new object[] { command, _pipelineContext, cancellationToken });

                                await task.ConfigureAwait(false);
                            }
                            else
                            {
                                throw new InvalidOperationException();
                            }
                        }

                        executedStep[i] = stepConfiguration.Id;

                        _pipelineConfiguration.AfterStep?.Invoke(_pipelineContext);
                    }
                    catch (Exception ex)
                    {
                        exceptions.Add(new PipelineException(ex.Message, stepConfiguration.Id, stepConfiguration.GetType().FullName, ex));
                        error = true;
                    }
                }

                if (error)
                {
                    throw new AggregatePipelineException(exceptions);
                }

                _pipelineConfiguration.AfterEnd?.Invoke(_pipelineContext);
            }
            finally
            {
                _pipelineConfiguration.AlwaysEnd?.Invoke(_pipelineContext);
            }


            return(true);
        }
Example #3
0
 protected virtual object GetPipelineObject(IStepConfiguration @object)
 {
     return(@object.Instance);
 }