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