// This is the main execute method called by the engine public async Task ExecuteAsync( Engine engine, Guid executionId, CancellationTokenSource cancellationTokenSource) { if (_disposed) { throw new ObjectDisposedException(nameof(PipelinePhase)); } if (_modules.Count == 0) { _logger.LogDebug($"Pipeline {PipelineName}/{Phase} contains no modules, skipping"); Outputs = GetInputs(); return; } System.Diagnostics.Stopwatch pipelineStopwatch = System.Diagnostics.Stopwatch.StartNew(); _logger.LogDebug($"Executing pipeline {PipelineName}/{Phase} with {_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(engine, executionId, this, serviceScope.ServiceProvider, cancellationTokenSource.Token); Outputs = await Engine.ExecuteModulesAsync(contextData, null, _modules, GetInputs(), _logger); pipelineStopwatch.Stop(); _logger.LogInformation($"Executed pipeline {PipelineName}/{Phase} in {pipelineStopwatch.ElapsedMilliseconds} ms resulting in {Outputs.Length} output document(s)"); } } catch (Exception ex) { if (!(ex is OperationCanceledException)) { _logger.LogCritical($"Exception while executing pipeline {PipelineName}/{Phase}: {ex}"); cancellationTokenSource.Cancel(); } Outputs = ImmutableArray <IDocument> .Empty; throw; } // Store the result documents, but only if this is the Process phase of a non-isolated pipeline if (!Pipeline.Isolated && Phase == Phase.Process) { engine.Documents.AddOrUpdate( PipelineName, Outputs, (_, __) => Outputs); } }
// This is the main execute method called by the engine public async Task ExecuteAsync(Engine engine, ConcurrentDictionary <string, PhaseResult[]> phaseResults) { if (_disposed) { throw new ObjectDisposedException(nameof(PipelinePhase)); } // Raise the before event await engine.Events.RaiseAsync(new BeforePipelinePhaseExecution(engine.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(); DateTimeOffset startTime = DateTimeOffset.Now; 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, phaseResults, serviceScope.ServiceProvider); 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(engine.ExecutionId, PipelineName, Phase, Outputs, stopwatch.ElapsedMilliseconds)); // Record the results PhaseResult phaseResult = new PhaseResult(PipelineName, Phase, Outputs, startTime, 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);