| | | 1 | | using Elsa.Abstractions; |
| | | 2 | | using Elsa.Workflows.Management; |
| | | 3 | | using Elsa.Workflows.Management.Filters; |
| | | 4 | | using Elsa.Workflows.Runtime; |
| | | 5 | | using JetBrains.Annotations; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Workflows.Api.Endpoints.WorkflowInstances.BulkDelete; |
| | | 8 | | |
| | | 9 | | [PublicAPI] |
| | 3 | 10 | | internal class BulkDelete(IWorkflowInstanceStore workflowInstanceStore, IWorkflowInstanceManager workflowInstanceManager |
| | | 11 | | { |
| | | 12 | | public override void Configure() |
| | | 13 | | { |
| | 1 | 14 | | Post( |
| | 1 | 15 | | "/bulk-actions/delete/workflow-instances", |
| | 1 | 16 | | "/bulk-actions/delete/workflow-instances/by-id" // Deprecated route. |
| | 1 | 17 | | ); |
| | 1 | 18 | | ConfigurePermissions("delete:workflow-instances"); |
| | 1 | 19 | | } |
| | | 20 | | |
| | | 21 | | public override async Task<Response> ExecuteAsync(Request request, CancellationToken cancellationToken) |
| | | 22 | | { |
| | 2 | 23 | | var baseFilter = new WorkflowInstanceFilter |
| | 2 | 24 | | { |
| | 2 | 25 | | Ids = request.Ids, |
| | 2 | 26 | | DefinitionId = request.WorkflowDefinitionId, |
| | 2 | 27 | | DefinitionIds = request.WorkflowDefinitionIds, |
| | 2 | 28 | | }; |
| | | 29 | | |
| | | 30 | | // Step 1: Delete running instances individually (requires coordination by the workflow runtime). |
| | 2 | 31 | | var runningFilter = new WorkflowInstanceFilter |
| | 2 | 32 | | { |
| | 2 | 33 | | Ids = baseFilter.Ids, |
| | 2 | 34 | | DefinitionId = baseFilter.DefinitionId, |
| | 2 | 35 | | DefinitionIds = baseFilter.DefinitionIds, |
| | 2 | 36 | | WorkflowStatus = WorkflowStatus.Running |
| | 2 | 37 | | }; |
| | | 38 | | |
| | 2 | 39 | | var runningInstanceIds = await workflowInstanceStore.FindManyIdsAsync(runningFilter, cancellationToken); |
| | 2 | 40 | | var count = 0L; |
| | | 41 | | |
| | 8 | 42 | | foreach (var instanceId in runningInstanceIds) |
| | | 43 | | { |
| | 2 | 44 | | var client = await workflowRuntime.CreateClientAsync(instanceId, cancellationToken); |
| | 2 | 45 | | var deleted = await client.DeleteAsync(cancellationToken); |
| | 2 | 46 | | if (deleted) |
| | 2 | 47 | | count++; |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | // Step 2: Bulk delete finished instances (no coordination needed). |
| | | 51 | | // Use IWorkflowInstanceManager to ensure related records are also deleted. |
| | 2 | 52 | | var finishedFilter = new WorkflowInstanceFilter |
| | 2 | 53 | | { |
| | 2 | 54 | | Ids = baseFilter.Ids, |
| | 2 | 55 | | DefinitionId = baseFilter.DefinitionId, |
| | 2 | 56 | | DefinitionIds = baseFilter.DefinitionIds, |
| | 2 | 57 | | WorkflowStatus = WorkflowStatus.Finished |
| | 2 | 58 | | }; |
| | | 59 | | |
| | 2 | 60 | | var finishedDeletedCount = await workflowInstanceManager.BulkDeleteAsync(finishedFilter, cancellationToken); |
| | 2 | 61 | | count += finishedDeletedCount; |
| | | 62 | | |
| | 2 | 63 | | return new(count); |
| | 2 | 64 | | } |
| | | 65 | | } |