// This is the main execute method called by the engine public async Task ExecuteAsync( Engine engine, Guid executionId, ConcurrentDictionary <string, PhaseResult[]> phaseResults, CancellationTokenSource cancellationTokenSource) { if (_disposed) { throw new ObjectDisposedException(nameof(PipelinePhase)); } // Raise the before event await engine.Events.RaiseAsync(new BeforePipelinePhaseExecution(executionId, PipelineName, Phase)); // Skip the phase if there are no modules if (_modules.Count == 0) { _logger.LogDebug($"{PipelineName}/{Phase} » Pipeline contains no modules, skipping"); Outputs = GetInputs(); return; } // Execute the phase ImmutableArray <IDocument> inputs = GetInputs(); System.Diagnostics.Stopwatch stopwatch = System.Diagnostics.Stopwatch.StartNew(); _logger.LogInformation($"-> {PipelineName}/{Phase} » Starting {PipelineName} {Phase} phase execution... ({inputs.Length} input document(s), {_modules.Count} module(s))"); try { // Execute all modules in the pipeline with a new DI scope per phase IServiceScopeFactory serviceScopeFactory = engine.Services.GetRequiredService <IServiceScopeFactory>(); using (IServiceScope serviceScope = serviceScopeFactory.CreateScope()) { ExecutionContextData contextData = new ExecutionContextData( this, engine, executionId, phaseResults, serviceScope.ServiceProvider, cancellationTokenSource.Token); Outputs = await Engine.ExecuteModulesAsync(contextData, null, _modules, inputs, _logger); stopwatch.Stop(); _logger.LogInformation($"-- {PipelineName}/{Phase} » Finished {PipelineName} {Phase} phase execution ({Outputs.Length} output document(s), {stopwatch.ElapsedMilliseconds} ms)"); } } catch (Exception ex) { if (!(ex is OperationCanceledException)) { _logger.LogCritical($"Exception while executing pipeline {PipelineName}/{Phase}: {ex}"); } Outputs = ImmutableArray <IDocument> .Empty; throw; } finally { stopwatch.Stop(); } // Raise the after event await engine.Events.RaiseAsync(new AfterPipelinePhaseExecution(executionId, PipelineName, Phase, Outputs, stopwatch.ElapsedMilliseconds)); // Record the results PhaseResult phaseResult = new PhaseResult(PipelineName, Phase, Outputs, stopwatch.ElapsedMilliseconds); phaseResults.AddOrUpdate( phaseResult.PipelineName, _ => { PhaseResult[] results = new PhaseResult[4]; results[(int)phaseResult.Phase] = phaseResult; return(results); }, (_, results) => { if (results[(int)phaseResult.Phase] != null) { // Sanity check, we should never hit this throw new InvalidOperationException($"Results for phase {phaseResult.Phase} have already been added"); } results[(int)phaseResult.Phase] = phaseResult; return(results); }); }
/// <inheritdoc/> public async Task <ImmutableArray <IDocument> > ExecuteModulesAsync(IEnumerable <IModule> modules, IEnumerable <IDocument> inputs) => await Engine.ExecuteModulesAsync(_contextData, this, modules, inputs?.ToImmutableArray() ?? ImmutableArray <IDocument> .Empty, this);