| | | 1 | | using Elsa.Alterations.Core.Contracts; |
| | | 2 | | using Elsa.Alterations.Core.Models; |
| | | 3 | | using Elsa.Alterations.Core.Results; |
| | | 4 | | using Elsa.Alterations.Middleware.Workflows; |
| | | 5 | | using Elsa.Common; |
| | | 6 | | using Elsa.Workflows; |
| | | 7 | | using Elsa.Workflows.CommitStates; |
| | | 8 | | using Elsa.Workflows.Management; |
| | | 9 | | using Elsa.Workflows.Pipelines.WorkflowExecution; |
| | | 10 | | using Elsa.Workflows.Runtime; |
| | | 11 | | using Microsoft.Extensions.Logging; |
| | | 12 | | |
| | | 13 | | namespace Elsa.Alterations.Services; |
| | | 14 | | |
| | | 15 | | /// <inheritdoc /> |
| | 2 | 16 | | public class DefaultAlterationRunner( |
| | 2 | 17 | | IWorkflowRuntime workflowRuntime, |
| | 2 | 18 | | IWorkflowExecutionPipeline workflowExecutionPipeline, |
| | 2 | 19 | | IWorkflowDefinitionService workflowDefinitionService, |
| | 2 | 20 | | IWorkflowStateExtractor workflowStateExtractor, |
| | 2 | 21 | | ICommitStateHandler commitStateHandler, |
| | 2 | 22 | | ISystemClock systemClock, |
| | 2 | 23 | | IServiceProvider serviceProvider) |
| | | 24 | | : IAlterationRunner |
| | | 25 | | { |
| | | 26 | | /// <inheritdoc /> |
| | | 27 | | public async Task<ICollection<RunAlterationsResult>> RunAsync(IEnumerable<string> workflowInstanceIds, IEnumerable<I |
| | | 28 | | { |
| | 1 | 29 | | var results = new List<RunAlterationsResult>(); |
| | 1 | 30 | | var alterationList = alterations as ICollection<IAlteration> ?? alterations.ToList(); |
| | | 31 | | |
| | 4 | 32 | | foreach (var workflowInstanceId in workflowInstanceIds) |
| | | 33 | | { |
| | 1 | 34 | | var result = await RunAsync(workflowInstanceId, alterationList, cancellationToken); |
| | 1 | 35 | | results.Add(result); |
| | | 36 | | } |
| | | 37 | | |
| | 1 | 38 | | return results; |
| | 1 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <inheritdoc /> |
| | | 42 | | public async Task<RunAlterationsResult> RunAsync(string workflowInstanceId, IEnumerable<IAlteration> alterations, Ca |
| | | 43 | | { |
| | 1 | 44 | | var log = new AlterationLog(systemClock); |
| | 1 | 45 | | var result = new RunAlterationsResult(workflowInstanceId, log); |
| | 1 | 46 | | var workflowClient = await workflowRuntime.CreateClientAsync(workflowInstanceId, cancellationToken: cancellation |
| | | 47 | | |
| | | 48 | | // Load workflow instance. |
| | 1 | 49 | | var workflowState = await workflowClient.ExportStateAsync(cancellationToken); |
| | | 50 | | |
| | 1 | 51 | | if (workflowState == null) |
| | | 52 | | { |
| | 0 | 53 | | log.Add($"Workflow instance with ID '{workflowInstanceId}' not found.", LogLevel.Error); |
| | 0 | 54 | | return result; |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | // Load workflow definition. |
| | 1 | 58 | | var workflowGraph = await workflowDefinitionService.FindWorkflowGraphAsync(workflowState.DefinitionVersionId, ca |
| | | 59 | | |
| | 1 | 60 | | if (workflowGraph == null) |
| | | 61 | | { |
| | 0 | 62 | | log.Add($"Workflow definition with ID '{workflowState.DefinitionVersionId}' not found.", LogLevel.Error); |
| | 0 | 63 | | return result; |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | // Create workflow execution context. |
| | 1 | 67 | | var workflowExecutionContext = await WorkflowExecutionContext.CreateAsync(serviceProvider, workflowGraph, workfl |
| | 1 | 68 | | workflowExecutionContext.TransientProperties.Add(RunAlterationsMiddleware.AlterationsPropertyKey, alterations); |
| | 1 | 69 | | workflowExecutionContext.TransientProperties.Add(RunAlterationsMiddleware.AlterationsLogPropertyKey, log); |
| | | 70 | | |
| | | 71 | | // Build a new workflow execution pipeline. |
| | 1 | 72 | | var pipelineBuilder = new WorkflowExecutionPipelineBuilder(serviceProvider); |
| | 1 | 73 | | workflowExecutionPipeline.ConfigurePipelineBuilder(pipelineBuilder); |
| | | 74 | | |
| | | 75 | | // Replace the terminal DefaultActivitySchedulerMiddleware with the RunAlterationsMiddleware terminal. |
| | 1 | 76 | | pipelineBuilder.ReplaceTerminal<RunAlterationsMiddleware>(); |
| | | 77 | | |
| | | 78 | | // Build modified pipeline. |
| | 1 | 79 | | var pipeline = pipelineBuilder.Build(); |
| | | 80 | | |
| | | 81 | | // Execute the pipeline. |
| | 1 | 82 | | await pipeline(workflowExecutionContext); |
| | | 83 | | |
| | | 84 | | // Extract workflow state. |
| | 1 | 85 | | workflowState = workflowStateExtractor.Extract(workflowExecutionContext); |
| | | 86 | | |
| | | 87 | | // Commit workflow state. |
| | 1 | 88 | | await commitStateHandler.CommitAsync(workflowExecutionContext, workflowState, cancellationToken); |
| | | 89 | | |
| | | 90 | | // Apply updated workflow state. |
| | | 91 | | // TODO: Importing back into the workflow runtime makes sense, but this also causes another SAVE ction of the wo |
| | | 92 | | // Can we avoid this? Perhaps we need more granular control over when we purge and when we save to DB. |
| | 1 | 93 | | await workflowClient.ImportStateAsync(workflowState, cancellationToken); |
| | | 94 | | |
| | | 95 | | // Check if the workflow has scheduled work. |
| | 1 | 96 | | result.WorkflowHasScheduledWork = workflowExecutionContext.Scheduler.HasAny; |
| | | 97 | | |
| | 1 | 98 | | return result; |
| | 1 | 99 | | } |
| | | 100 | | } |