| | | 1 | | using Elsa.Common.Models; |
| | | 2 | | using Elsa.Workflows.Management; |
| | | 3 | | using Elsa.Workflows.Management.Entities; |
| | | 4 | | using Elsa.Workflows.Management.Filters; |
| | | 5 | | using Elsa.Workflows.Runtime.Requests; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Workflows.Runtime; |
| | | 8 | | |
| | | 9 | | /// <inheritdoc /> |
| | 1 | 10 | | public class WorkflowCancellationService( |
| | 1 | 11 | | IWorkflowDefinitionService workflowDefinitionService, |
| | 1 | 12 | | IWorkflowInstanceStore workflowInstanceStore, |
| | 1 | 13 | | IWorkflowCancellationDispatcher dispatcher) |
| | | 14 | | : IWorkflowCancellationService |
| | | 15 | | { |
| | | 16 | | /// <inheritdoc /> |
| | | 17 | | public async Task<bool> CancelWorkflowAsync(string workflowInstanceId, CancellationToken cancellationToken = default |
| | | 18 | | { |
| | 0 | 19 | | var filter = new WorkflowInstanceFilter |
| | 0 | 20 | | { |
| | 0 | 21 | | Id = workflowInstanceId |
| | 0 | 22 | | }; |
| | 0 | 23 | | var instance = await workflowInstanceStore.FindAsync(filter, cancellationToken); |
| | | 24 | | |
| | 0 | 25 | | if(instance == null) |
| | 0 | 26 | | return false; |
| | | 27 | | |
| | 0 | 28 | | await CancelWorkflows([instance], cancellationToken); |
| | 0 | 29 | | return true; |
| | 0 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <inheritdoc /> |
| | | 33 | | public async Task<int> CancelWorkflowsAsync(IEnumerable<string> workflowInstanceIds, CancellationToken cancellationT |
| | | 34 | | { |
| | 0 | 35 | | var filter = new WorkflowInstanceFilter |
| | 0 | 36 | | { |
| | 0 | 37 | | Ids = workflowInstanceIds.ToList() |
| | 0 | 38 | | }; |
| | 0 | 39 | | var instances = await workflowInstanceStore.FindManyAsync(filter, cancellationToken); |
| | 0 | 40 | | return await CancelWorkflows(instances.ToList(), cancellationToken); |
| | 0 | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <inheritdoc /> |
| | | 44 | | public async Task<int> CancelWorkflowByDefinitionVersionAsync(string definitionVersionId, CancellationToken cancella |
| | | 45 | | { |
| | 0 | 46 | | var filter = new WorkflowInstanceFilter |
| | 0 | 47 | | { |
| | 0 | 48 | | DefinitionVersionId = definitionVersionId, |
| | 0 | 49 | | WorkflowStatus = WorkflowStatus.Running |
| | 0 | 50 | | }; |
| | 0 | 51 | | var instances = (await workflowInstanceStore.FindManyAsync(filter, cancellationToken)).ToList(); |
| | 0 | 52 | | var instanceIds = instances.Select(i => i.Id).ToList(); |
| | | 53 | | |
| | 0 | 54 | | return await CancelWorkflowsAsync(instanceIds, cancellationToken); |
| | 0 | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <inheritdoc /> |
| | | 58 | | public async Task<int> CancelWorkflowByDefinitionAsync(string definitionId, VersionOptions versionOptions, Cancellat |
| | | 59 | | { |
| | | 60 | | // Shouldn't we get possible multiple definitions here? |
| | 0 | 61 | | var workflowDefinition = await workflowDefinitionService.FindWorkflowDefinitionAsync(definitionId, versionOption |
| | 0 | 62 | | if (workflowDefinition is null) |
| | 0 | 63 | | return 0; |
| | | 64 | | |
| | 0 | 65 | | return await CancelWorkflowByDefinitionVersionAsync(workflowDefinition.Id, cancellationToken); |
| | 0 | 66 | | } |
| | | 67 | | |
| | | 68 | | private async Task<int> CancelWorkflows(IList<WorkflowInstance> workflowInstances, CancellationToken cancellationTok |
| | | 69 | | { |
| | 0 | 70 | | var tasks = workflowInstances.Where(i => i.Status != WorkflowStatus.Finished) |
| | 0 | 71 | | .Select(i => dispatcher.DispatchAsync(new DispatchCancelWorkflowRequest |
| | 0 | 72 | | { |
| | 0 | 73 | | WorkflowInstanceId = i.Id |
| | 0 | 74 | | }, cancellationToken)).ToList(); |
| | | 75 | | |
| | 0 | 76 | | var instanceIds = workflowInstances.Select(i => i.Id).ToList(); |
| | 0 | 77 | | await CancelChildWorkflowInstances(instanceIds, cancellationToken); |
| | 0 | 78 | | await Task.WhenAll(tasks); |
| | | 79 | | |
| | 0 | 80 | | return tasks.Count; |
| | 0 | 81 | | } |
| | | 82 | | |
| | | 83 | | private async Task CancelChildWorkflowInstances(IEnumerable<string> workflowInstanceIds, CancellationToken cancellat |
| | | 84 | | { |
| | 0 | 85 | | var tasks = new List<Task<int>>(); |
| | 0 | 86 | | var workflowInstanceIdBatches = workflowInstanceIds.Chunk(50); |
| | | 87 | | |
| | 0 | 88 | | foreach (var workflowInstanceIdBatch in workflowInstanceIdBatches) |
| | | 89 | | { |
| | | 90 | | // Find child instances for the current workflow instance ID and cancel them. |
| | | 91 | | // Do not check on status as their children might still be running and need to be canceled. |
| | 0 | 92 | | WorkflowInstanceFilter filter = new() |
| | 0 | 93 | | { |
| | 0 | 94 | | ParentWorkflowInstanceIds = workflowInstanceIdBatch.ToList() |
| | 0 | 95 | | }; |
| | 0 | 96 | | var childInstances = (await workflowInstanceStore.FindManyAsync(filter, cancellationToken)).ToList(); |
| | | 97 | | |
| | 0 | 98 | | if (childInstances.Any()) |
| | 0 | 99 | | tasks.Add(CancelWorkflows(childInstances, cancellationToken)); |
| | | 100 | | } |
| | | 101 | | |
| | 0 | 102 | | await Task.WhenAll(tasks); |
| | 0 | 103 | | } |
| | | 104 | | } |