| | | 1 | | using Elsa.Workflows.Management; |
| | | 2 | | using Elsa.Workflows.Management.Entities; |
| | | 3 | | using Elsa.Workflows.Management.Exceptions; |
| | | 4 | | using Elsa.Workflows.Management.Filters; |
| | | 5 | | using Elsa.Workflows.Management.Mappers; |
| | | 6 | | using Elsa.Workflows.Management.Options; |
| | | 7 | | using Elsa.Workflows.Models; |
| | | 8 | | using Elsa.Workflows.Options; |
| | | 9 | | using Elsa.Workflows.Runtime.Exceptions; |
| | | 10 | | using Elsa.Workflows.Runtime.Messages; |
| | | 11 | | using Elsa.Workflows.State; |
| | | 12 | | using Microsoft.Extensions.Logging; |
| | | 13 | | |
| | | 14 | | namespace Elsa.Workflows.Runtime; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Represents a client for executing and managing local workflows. |
| | | 18 | | /// </summary> |
| | 188 | 19 | | public class LocalWorkflowClient( |
| | 188 | 20 | | string workflowInstanceId, |
| | 188 | 21 | | IWorkflowInstanceManager workflowInstanceManager, |
| | 188 | 22 | | IWorkflowDefinitionService workflowDefinitionService, |
| | 188 | 23 | | IWorkflowRunner workflowRunner, |
| | 188 | 24 | | IWorkflowCanceler workflowCanceler, |
| | 188 | 25 | | WorkflowStateMapper workflowStateMapper, |
| | 188 | 26 | | ILogger<LocalWorkflowClient> logger) : IWorkflowClient |
| | | 27 | | { |
| | | 28 | | /// <inheritdoc /> |
| | 448 | 29 | | public string WorkflowInstanceId => workflowInstanceId; |
| | | 30 | | |
| | | 31 | | /// <inheritdoc /> |
| | | 32 | | public async Task<CreateWorkflowInstanceResponse> CreateInstanceAsync(CreateWorkflowInstanceRequest request, Cancell |
| | | 33 | | { |
| | 36 | 34 | | var workflowDefinitionHandle = request.WorkflowDefinitionHandle; |
| | 36 | 35 | | var workflowGraph = await GetWorkflowGraphAsync(workflowDefinitionHandle, cancellationToken); |
| | | 36 | | |
| | 34 | 37 | | var options = new WorkflowInstanceOptions |
| | 34 | 38 | | { |
| | 34 | 39 | | WorkflowInstanceId = WorkflowInstanceId, |
| | 34 | 40 | | CorrelationId = request.CorrelationId, |
| | 34 | 41 | | Name = request.Name, |
| | 34 | 42 | | ParentWorkflowInstanceId = request.ParentId, |
| | 34 | 43 | | Input = request.Input, |
| | 34 | 44 | | Properties = request.Properties |
| | 34 | 45 | | }; |
| | | 46 | | |
| | 34 | 47 | | await workflowInstanceManager.CreateAndCommitWorkflowInstanceAsync(workflowGraph.Workflow, options, cancellation |
| | 34 | 48 | | return new(); |
| | 34 | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <inheritdoc /> |
| | | 52 | | public async Task<RunWorkflowInstanceResponse> RunInstanceAsync(RunWorkflowInstanceRequest request, CancellationToke |
| | | 53 | | { |
| | 78 | 54 | | var workflowInstance = await GetWorkflowInstanceAsync(cancellationToken); |
| | 78 | 55 | | return await RunInstanceAsync(workflowInstance, request, cancellationToken); |
| | 78 | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <inheritdoc /> |
| | | 59 | | public async Task<RunWorkflowInstanceResponse> CreateAndRunInstanceAsync(CreateAndRunWorkflowInstanceRequest request |
| | | 60 | | { |
| | 46 | 61 | | var createRequest = new CreateWorkflowInstanceRequest |
| | 46 | 62 | | { |
| | 46 | 63 | | Properties = request.Properties, |
| | 46 | 64 | | CorrelationId = request.CorrelationId, |
| | 46 | 65 | | Name = request.Name, |
| | 46 | 66 | | Input = request.Input, |
| | 46 | 67 | | WorkflowDefinitionHandle = request.WorkflowDefinitionHandle, |
| | 46 | 68 | | ParentId = request.ParentId |
| | 46 | 69 | | }; |
| | 46 | 70 | | var workflowInstance = await CreateInstanceInternalAsync(createRequest, cancellationToken); |
| | 44 | 71 | | return await RunInstanceAsync(workflowInstance, new() |
| | 44 | 72 | | { |
| | 44 | 73 | | Input = request.Input, |
| | 44 | 74 | | Variables = request.Variables, |
| | 44 | 75 | | Properties = request.Properties, |
| | 44 | 76 | | TriggerActivityId = request.TriggerActivityId, |
| | 44 | 77 | | ActivityHandle = request.ActivityHandle, |
| | 44 | 78 | | IncludeWorkflowOutput = request.IncludeWorkflowOutput |
| | 44 | 79 | | }, cancellationToken); |
| | 44 | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <inheritdoc /> |
| | | 83 | | public async Task CancelAsync(CancellationToken cancellationToken = default) |
| | | 84 | | { |
| | 1 | 85 | | var workflowInstance = await GetWorkflowInstanceAsync(cancellationToken); |
| | 1 | 86 | | await CancelAsync(workflowInstance, cancellationToken); |
| | 1 | 87 | | } |
| | | 88 | | |
| | | 89 | | private async Task CancelAsync(WorkflowInstance workflowInstance, CancellationToken cancellationToken) |
| | | 90 | | { |
| | 11 | 91 | | if (workflowInstance.Status != WorkflowStatus.Running) return; |
| | 5 | 92 | | var workflowGraph = await GetWorkflowGraphAsync(workflowInstance, cancellationToken); |
| | 5 | 93 | | var workflowState = await workflowCanceler.CancelWorkflowAsync(workflowGraph, workflowInstance.WorkflowState, ca |
| | 5 | 94 | | await workflowInstanceManager.SaveAsync(workflowState, cancellationToken); |
| | 8 | 95 | | } |
| | | 96 | | |
| | | 97 | | /// <inheritdoc /> |
| | | 98 | | public async Task<WorkflowState> ExportStateAsync(CancellationToken cancellationToken = default) |
| | | 99 | | { |
| | 55 | 100 | | var workflowInstance = await GetWorkflowInstanceAsync(cancellationToken); |
| | 55 | 101 | | return workflowInstance.WorkflowState; |
| | 55 | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <inheritdoc /> |
| | | 105 | | public async Task ImportStateAsync(WorkflowState workflowState, CancellationToken cancellationToken = default) |
| | | 106 | | { |
| | 1 | 107 | | var workflowInstance = workflowStateMapper.Map(workflowState)!; |
| | 1 | 108 | | await workflowInstanceManager.SaveAsync(workflowInstance, cancellationToken); |
| | 1 | 109 | | } |
| | | 110 | | |
| | | 111 | | public Task<bool> InstanceExistsAsync(CancellationToken cancellationToken = default) |
| | | 112 | | { |
| | 0 | 113 | | return workflowInstanceManager.ExistsAsync(workflowInstanceId, cancellationToken); |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | /// <inheritdoc /> |
| | | 117 | | public async Task<bool> DeleteAsync(CancellationToken cancellationToken = default) |
| | | 118 | | { |
| | | 119 | | // Load the workflow instance (single DB call) |
| | 8 | 120 | | var workflowInstance = await TryGetWorkflowInstanceAsync(cancellationToken); |
| | 8 | 121 | | if (workflowInstance == null) |
| | 1 | 122 | | return false; |
| | | 123 | | |
| | 7 | 124 | | await CancelAsync(workflowInstance, cancellationToken); |
| | | 125 | | |
| | | 126 | | // Delete the workflow instance |
| | 7 | 127 | | var filter = new WorkflowInstanceFilter { Id = workflowInstanceId }; |
| | 7 | 128 | | await workflowInstanceManager.DeleteAsync(filter, cancellationToken); |
| | 7 | 129 | | return true; |
| | 8 | 130 | | } |
| | | 131 | | |
| | | 132 | | public async Task<RunWorkflowInstanceResponse> RunInstanceAsync(WorkflowInstance workflowInstance, RunWorkflowInstan |
| | | 133 | | { |
| | 175 | 134 | | var workflowState = workflowInstance.WorkflowState; |
| | | 135 | | |
| | 175 | 136 | | if (workflowInstance.Status != WorkflowStatus.Running) |
| | | 137 | | { |
| | 0 | 138 | | logger.LogWarning("Attempt to resume workflow {WorkflowInstanceId} that is not in the Running state. The act |
| | 0 | 139 | | return new() |
| | 0 | 140 | | { |
| | 0 | 141 | | WorkflowInstanceId = WorkflowInstanceId, |
| | 0 | 142 | | Status = workflowInstance.Status, |
| | 0 | 143 | | SubStatus = workflowInstance.SubStatus |
| | 0 | 144 | | }; |
| | | 145 | | } |
| | | 146 | | |
| | 175 | 147 | | var runWorkflowOptions = new RunWorkflowOptions |
| | 175 | 148 | | { |
| | 175 | 149 | | Input = request.Input, |
| | 175 | 150 | | Variables = request.Variables, |
| | 175 | 151 | | Properties = request.Properties, |
| | 175 | 152 | | BookmarkId = request.BookmarkId, |
| | 175 | 153 | | TriggerActivityId = request.TriggerActivityId, |
| | 175 | 154 | | ActivityHandle = request.ActivityHandle, |
| | 175 | 155 | | }; |
| | | 156 | | |
| | 175 | 157 | | var workflowGraph = await GetWorkflowGraphAsync(workflowInstance, cancellationToken); |
| | 175 | 158 | | var workflowResult = await workflowRunner.RunAsync(workflowGraph, workflowState, runWorkflowOptions, cancellatio |
| | | 159 | | |
| | 175 | 160 | | workflowState = workflowResult.WorkflowState; |
| | | 161 | | |
| | 175 | 162 | | return new() |
| | 175 | 163 | | { |
| | 175 | 164 | | WorkflowInstanceId = WorkflowInstanceId, |
| | 175 | 165 | | Status = workflowState.Status, |
| | 175 | 166 | | SubStatus = workflowState.SubStatus, |
| | 175 | 167 | | Incidents = workflowState.Incidents, |
| | 175 | 168 | | Output = request.IncludeWorkflowOutput ? new Dictionary<string, object>(workflowState.Output) : null, |
| | 175 | 169 | | Bookmarks = workflowState.Bookmarks |
| | 175 | 170 | | }; |
| | 175 | 171 | | } |
| | | 172 | | |
| | | 173 | | public async Task<WorkflowInstance> CreateInstanceInternalAsync(CreateWorkflowInstanceRequest request, CancellationT |
| | | 174 | | { |
| | 99 | 175 | | var workflowDefinitionHandle = request.WorkflowDefinitionHandle; |
| | 99 | 176 | | var workflowGraph = await GetWorkflowGraphAsync(workflowDefinitionHandle, cancellationToken); |
| | | 177 | | |
| | 97 | 178 | | var options = new WorkflowInstanceOptions |
| | 97 | 179 | | { |
| | 97 | 180 | | WorkflowInstanceId = WorkflowInstanceId, |
| | 97 | 181 | | CorrelationId = request.CorrelationId, |
| | 97 | 182 | | Name = request.Name, |
| | 97 | 183 | | ParentWorkflowInstanceId = request.ParentId, |
| | 97 | 184 | | Input = request.Input, |
| | 97 | 185 | | Properties = request.Properties |
| | 97 | 186 | | }; |
| | | 187 | | |
| | 97 | 188 | | return workflowInstanceManager.CreateWorkflowInstance(workflowGraph.Workflow, options); |
| | 97 | 189 | | } |
| | | 190 | | |
| | | 191 | | private async Task<WorkflowInstance> GetWorkflowInstanceAsync(CancellationToken cancellationToken) |
| | | 192 | | { |
| | 134 | 193 | | var workflowInstance = await TryGetWorkflowInstanceAsync(cancellationToken); |
| | 134 | 194 | | if (workflowInstance == null) throw new WorkflowInstanceNotFoundException("Workflow instance not found.", Workfl |
| | 134 | 195 | | return workflowInstance; |
| | 134 | 196 | | } |
| | | 197 | | |
| | | 198 | | private Task<WorkflowInstance?> TryGetWorkflowInstanceAsync(CancellationToken cancellationToken) |
| | | 199 | | { |
| | 142 | 200 | | return workflowInstanceManager.FindByIdAsync(WorkflowInstanceId, cancellationToken); |
| | | 201 | | } |
| | | 202 | | |
| | | 203 | | private async Task<WorkflowGraph> GetWorkflowGraphAsync(WorkflowInstance workflowInstance, CancellationToken cancell |
| | | 204 | | { |
| | 180 | 205 | | var handle = WorkflowDefinitionHandle.ByDefinitionVersionId(workflowInstance.DefinitionVersionId); |
| | 180 | 206 | | return await GetWorkflowGraphAsync(handle, cancellationToken); |
| | 180 | 207 | | } |
| | | 208 | | |
| | | 209 | | private async Task<WorkflowGraph> GetWorkflowGraphAsync(WorkflowDefinitionHandle definitionHandle, CancellationToken |
| | | 210 | | { |
| | 315 | 211 | | var result = await workflowDefinitionService.TryFindWorkflowGraphAsync(definitionHandle, cancellationToken); |
| | 317 | 212 | | if (!result.WorkflowDefinitionExists) throw new WorkflowDefinitionNotFoundException("Workflow definition not fou |
| | 315 | 213 | | if (!result.WorkflowGraphExists) throw new WorkflowMaterializerNotFoundException(result.WorkflowDefinition!.Mate |
| | 311 | 214 | | return result.WorkflowGraph!; |
| | 311 | 215 | | } |
| | | 216 | | } |