| | | 1 | | using Elsa.Extensions; |
| | | 2 | | using Elsa.Mediator.Contracts; |
| | | 3 | | using Elsa.Workflows.Activities; |
| | | 4 | | using Elsa.Workflows.Management.Contracts; |
| | | 5 | | using Elsa.Workflows.Management.Entities; |
| | | 6 | | using Elsa.Workflows.Management.Filters; |
| | | 7 | | using Elsa.Workflows.Management.Mappers; |
| | | 8 | | using Elsa.Workflows.Management.Notifications; |
| | | 9 | | using Elsa.Workflows.Management.Options; |
| | | 10 | | using Elsa.Workflows.State; |
| | | 11 | | |
| | | 12 | | namespace Elsa.Workflows.Management.Services; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | 321 | 15 | | public class WorkflowInstanceManager( |
| | 321 | 16 | | IWorkflowInstanceStore store, |
| | 321 | 17 | | IWorkflowInstanceFactory workflowInstanceFactory, |
| | 321 | 18 | | INotificationSender notificationSender, |
| | 321 | 19 | | WorkflowStateMapper workflowStateMapper, |
| | 321 | 20 | | IWorkflowStateExtractor workflowStateExtractor, |
| | 321 | 21 | | IWorkflowStateSerializer workflowStateSerializer) |
| | | 22 | | : IWorkflowInstanceManager |
| | | 23 | | { |
| | | 24 | | /// <inheritdoc /> |
| | | 25 | | public async Task<WorkflowInstance?> FindByIdAsync(string instanceId, CancellationToken cancellationToken = default) |
| | | 26 | | { |
| | 76 | 27 | | return await store.FindAsync(instanceId, cancellationToken); |
| | 76 | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <inheritdoc /> |
| | | 31 | | public async Task<WorkflowInstance?> FindAsync(WorkflowInstanceFilter filter, CancellationToken cancellationToken = |
| | | 32 | | { |
| | 0 | 33 | | return await store.FindAsync(filter, cancellationToken); |
| | 0 | 34 | | } |
| | | 35 | | |
| | | 36 | | public async Task<bool> ExistsAsync(string instanceId, CancellationToken cancellationToken = default) |
| | | 37 | | { |
| | 0 | 38 | | var filter = new WorkflowInstanceFilter |
| | 0 | 39 | | { |
| | 0 | 40 | | Id = instanceId |
| | 0 | 41 | | }; |
| | 0 | 42 | | var count = await store.CountAsync(filter, cancellationToken); |
| | 0 | 43 | | return count > 0; |
| | 0 | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <inheritdoc /> |
| | | 47 | | public async Task SaveAsync(WorkflowInstance workflowInstance, CancellationToken cancellationToken = default) |
| | | 48 | | { |
| | 349 | 49 | | await store.SaveAsync(workflowInstance, cancellationToken); |
| | 349 | 50 | | await notificationSender.SendAsync(new WorkflowInstanceSaved(workflowInstance), cancellationToken); |
| | 349 | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <inheritdoc /> |
| | | 54 | | public async Task<WorkflowInstance> SaveAsync(WorkflowState workflowState, CancellationToken cancellationToken) |
| | | 55 | | { |
| | 327 | 56 | | var workflowInstance = workflowStateMapper.Map(workflowState)!; |
| | 327 | 57 | | await SaveAsync(workflowInstance, cancellationToken); |
| | 327 | 58 | | return workflowInstance; |
| | 327 | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <inheritdoc /> |
| | | 62 | | public async Task<WorkflowInstance> SaveAsync(WorkflowExecutionContext workflowExecutionContext, CancellationToken c |
| | | 63 | | { |
| | 0 | 64 | | var workflowState = ExtractWorkflowState(workflowExecutionContext); |
| | 0 | 65 | | return await SaveAsync(workflowState, cancellationToken); |
| | 0 | 66 | | } |
| | | 67 | | |
| | | 68 | | public async Task CreateAsync(WorkflowInstance workflowInstance, CancellationToken cancellationToken = default) |
| | | 69 | | { |
| | 0 | 70 | | await store.AddAsync(workflowInstance, cancellationToken); |
| | 0 | 71 | | await notificationSender.SendAsync(new WorkflowInstanceSaved(workflowInstance), cancellationToken); |
| | 0 | 72 | | } |
| | | 73 | | |
| | | 74 | | public async Task<WorkflowInstance> CreateAsync(WorkflowState workflowState, CancellationToken cancellationToken) |
| | | 75 | | { |
| | 0 | 76 | | var workflowInstance = workflowStateMapper.Map(workflowState)!; |
| | 0 | 77 | | await CreateAsync(workflowInstance, cancellationToken); |
| | 0 | 78 | | return workflowInstance; |
| | 0 | 79 | | } |
| | | 80 | | |
| | | 81 | | public async Task<WorkflowInstance> CreateAsync(WorkflowExecutionContext workflowExecutionContext, CancellationToken |
| | | 82 | | { |
| | 0 | 83 | | var workflowState = ExtractWorkflowState(workflowExecutionContext); |
| | 0 | 84 | | return await CreateAsync(workflowState, cancellationToken); |
| | 0 | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <inheritdoc /> |
| | | 88 | | public async Task UpdateAsync(WorkflowInstance workflowInstance, CancellationToken cancellationToken = default) |
| | | 89 | | { |
| | 0 | 90 | | await store.UpdateAsync(workflowInstance, cancellationToken); |
| | 0 | 91 | | await notificationSender.SendAsync(new WorkflowInstanceSaved(workflowInstance), cancellationToken); |
| | 0 | 92 | | } |
| | | 93 | | |
| | | 94 | | public async Task<WorkflowInstance> UpdateAsync(WorkflowState workflowState, CancellationToken cancellationToken) |
| | | 95 | | { |
| | 0 | 96 | | var workflowInstance = workflowStateMapper.Map(workflowState)!; |
| | 0 | 97 | | await UpdateAsync(workflowInstance, cancellationToken); |
| | 0 | 98 | | return workflowInstance; |
| | 0 | 99 | | } |
| | | 100 | | |
| | | 101 | | /// <inheritdoc /> |
| | | 102 | | public async Task<bool> DeleteAsync(WorkflowInstanceFilter filter, CancellationToken cancellationToken = default) |
| | | 103 | | { |
| | 7 | 104 | | var instance = await store.FindAsync(filter, cancellationToken); |
| | | 105 | | |
| | 7 | 106 | | if (instance == null) |
| | 0 | 107 | | return false; |
| | | 108 | | |
| | 7 | 109 | | var ids = new[] |
| | 7 | 110 | | { |
| | 7 | 111 | | instance.Id |
| | 7 | 112 | | }; |
| | 7 | 113 | | await notificationSender.SendAsync(new WorkflowInstancesDeleting(ids), cancellationToken); |
| | 7 | 114 | | await store.DeleteAsync(filter, cancellationToken); |
| | 7 | 115 | | await notificationSender.SendAsync(new WorkflowInstancesDeleted(ids), cancellationToken); |
| | 7 | 116 | | return true; |
| | 7 | 117 | | } |
| | | 118 | | |
| | | 119 | | /// <inheritdoc /> |
| | | 120 | | public async Task<long> BulkDeleteAsync(WorkflowInstanceFilter filter, CancellationToken cancellationToken = default |
| | | 121 | | { |
| | 6 | 122 | | var summaries = (await store.SummarizeManyAsync(filter, cancellationToken)).ToList(); |
| | 9 | 123 | | var ids = summaries.Select(x => x.Id).ToList(); |
| | 6 | 124 | | await notificationSender.SendAsync(new WorkflowInstancesDeleting(ids), cancellationToken); |
| | 6 | 125 | | var count = await store.DeleteAsync(filter, cancellationToken); |
| | 6 | 126 | | await notificationSender.SendAsync(new WorkflowInstancesDeleted(ids), cancellationToken); |
| | 6 | 127 | | return count; |
| | 6 | 128 | | } |
| | | 129 | | |
| | | 130 | | /// <inheritdoc /> |
| | | 131 | | public WorkflowState ExtractWorkflowState(WorkflowExecutionContext workflowExecutionContext) |
| | | 132 | | { |
| | 0 | 133 | | return workflowStateExtractor.Extract(workflowExecutionContext); |
| | | 134 | | } |
| | | 135 | | |
| | | 136 | | /// <inheritdoc /> |
| | | 137 | | public string SerializeWorkflowState(WorkflowState workflowState) |
| | | 138 | | { |
| | 0 | 139 | | return workflowStateSerializer.Serialize(workflowState); |
| | | 140 | | } |
| | | 141 | | |
| | | 142 | | /// <inheritdoc /> |
| | | 143 | | public async Task<WorkflowInstance> CreateAndCommitWorkflowInstanceAsync(Workflow workflow, WorkflowInstanceOptions? |
| | | 144 | | { |
| | 22 | 145 | | var workflowInstance = CreateWorkflowInstance(workflow, options); |
| | 22 | 146 | | await SaveAsync(workflowInstance, cancellationToken); |
| | 22 | 147 | | return workflowInstance; |
| | 22 | 148 | | } |
| | | 149 | | |
| | | 150 | | /// <inheritdoc /> |
| | | 151 | | public WorkflowInstance CreateWorkflowInstance(Workflow workflow, WorkflowInstanceOptions? options = null) |
| | | 152 | | { |
| | 72 | 153 | | return workflowInstanceFactory.CreateWorkflowInstance(workflow, options); |
| | | 154 | | } |
| | | 155 | | } |