// 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); }); }
static string GetPhaseResultTableString(PhaseResult result) => result == null ? string.Empty : $"{result.Outputs.Length} ({result.ElapsedMilliseconds} ms)";