| | | 1 | | using Elsa.Common.Multitenancy; |
| | | 2 | | using Elsa.Mediator; |
| | | 3 | | using Elsa.Mediator.Contracts; |
| | | 4 | | using Elsa.Tenants.Mediator; |
| | | 5 | | using Elsa.Workflows.Runtime.Commands; |
| | | 6 | | using Elsa.Workflows.Runtime.Notifications; |
| | | 7 | | using Elsa.Workflows.Runtime.Requests; |
| | | 8 | | using Elsa.Workflows.Runtime.Responses; |
| | | 9 | | |
| | | 10 | | namespace Elsa.Workflows.Runtime; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// A simple implementation that queues the specified request for workflow execution on a non-durable background worker. |
| | | 14 | | /// </summary> |
| | 485 | 15 | | public class BackgroundWorkflowDispatcher(ICommandSender commandSender, INotificationSender notificationSender, ITenantA |
| | | 16 | | { |
| | | 17 | | /// <inheritdoc /> |
| | | 18 | | public async Task<DispatchWorkflowResponse> DispatchAsync(DispatchWorkflowDefinitionRequest request, DispatchWorkflo |
| | | 19 | | { |
| | | 20 | | // Emit dispatching notification |
| | 28 | 21 | | await notificationSender.SendAsync(new WorkflowDefinitionDispatching(request), cancellationToken); |
| | | 22 | | |
| | 28 | 23 | | var command = new DispatchWorkflowDefinitionCommand(request.DefinitionVersionId) |
| | 28 | 24 | | { |
| | 28 | 25 | | Input = request.Input, |
| | 28 | 26 | | Properties = request.Properties, |
| | 28 | 27 | | CorrelationId = request.CorrelationId, |
| | 28 | 28 | | InstanceId = request.InstanceId, |
| | 28 | 29 | | TriggerActivityId = request.TriggerActivityId, |
| | 28 | 30 | | ParentWorkflowInstanceId = request.ParentWorkflowInstanceId, |
| | 28 | 31 | | SchedulingActivityExecutionId = request.SchedulingActivityExecutionId, |
| | 28 | 32 | | SchedulingWorkflowInstanceId = request.SchedulingWorkflowInstanceId, |
| | 28 | 33 | | SchedulingCallStackDepth = request.SchedulingCallStackDepth |
| | 28 | 34 | | }; |
| | | 35 | | |
| | | 36 | | // Background commands run independently of caller's lifecycle. |
| | 28 | 37 | | await commandSender.SendAsync(command, CommandStrategy.Background, CreateHeaders(), CancellationToken.None); |
| | 28 | 38 | | var response = DispatchWorkflowResponse.Success(); |
| | | 39 | | |
| | | 40 | | // Emit dispatched notification |
| | 28 | 41 | | await notificationSender.SendAsync(new WorkflowDefinitionDispatched(request, response), cancellationToken); |
| | | 42 | | |
| | 28 | 43 | | return response; |
| | 28 | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <inheritdoc /> |
| | | 47 | | public async Task<DispatchWorkflowResponse> DispatchAsync(DispatchWorkflowInstanceRequest request, DispatchWorkflowO |
| | | 48 | | { |
| | | 49 | | // Emit dispatching notification |
| | 2 | 50 | | await notificationSender.SendAsync(new WorkflowInstanceDispatching(request), cancellationToken); |
| | | 51 | | |
| | 2 | 52 | | var command = new DispatchWorkflowInstanceCommand(request.InstanceId){ |
| | 2 | 53 | | BookmarkId = request.BookmarkId, |
| | 2 | 54 | | ActivityHandle = request.ActivityHandle, |
| | 2 | 55 | | Input = request.Input, |
| | 2 | 56 | | Properties = request.Properties, |
| | 2 | 57 | | CorrelationId = request.CorrelationId}; |
| | | 58 | | |
| | | 59 | | // Background commands run independently of caller's lifecycle. |
| | 2 | 60 | | await commandSender.SendAsync(command, CommandStrategy.Background, CreateHeaders(), CancellationToken.None); |
| | 2 | 61 | | var response = DispatchWorkflowResponse.Success(); |
| | | 62 | | |
| | | 63 | | // Emit dispatched notification |
| | 2 | 64 | | await notificationSender.SendAsync(new WorkflowInstanceDispatched(request, response), cancellationToken); |
| | | 65 | | |
| | 2 | 66 | | return response; |
| | 2 | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <inheritdoc /> |
| | | 70 | | public async Task<DispatchWorkflowResponse> DispatchAsync(DispatchTriggerWorkflowsRequest request, DispatchWorkflowO |
| | | 71 | | { |
| | 1 | 72 | | var command = new DispatchTriggerWorkflowsCommand(request.ActivityTypeName, request.BookmarkPayload) |
| | 1 | 73 | | { |
| | 1 | 74 | | CorrelationId = request.CorrelationId, |
| | 1 | 75 | | WorkflowInstanceId = request.WorkflowInstanceId, |
| | 1 | 76 | | ActivityInstanceId = request.ActivityInstanceId, |
| | 1 | 77 | | Input = request.Input, |
| | 1 | 78 | | Properties = request.Properties |
| | 1 | 79 | | }; |
| | | 80 | | |
| | | 81 | | // Background commands run independently of caller's lifecycle. |
| | 1 | 82 | | await commandSender.SendAsync(command, CommandStrategy.Background, CreateHeaders(), CancellationToken.None); |
| | 1 | 83 | | return DispatchWorkflowResponse.Success(); |
| | 1 | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <inheritdoc /> |
| | | 87 | | public async Task<DispatchWorkflowResponse> DispatchAsync(DispatchResumeWorkflowsRequest request, DispatchWorkflowOp |
| | | 88 | | { |
| | 1 | 89 | | var command = new DispatchResumeWorkflowsCommand(request.ActivityTypeName, request.BookmarkPayload) |
| | 1 | 90 | | { |
| | 1 | 91 | | CorrelationId = request.CorrelationId, |
| | 1 | 92 | | WorkflowInstanceId = request.WorkflowInstanceId, |
| | 1 | 93 | | ActivityInstanceId = request.ActivityInstanceId, |
| | 1 | 94 | | Input = request.Input |
| | 1 | 95 | | }; |
| | | 96 | | |
| | | 97 | | // Background commands run independently of caller's lifecycle. |
| | 1 | 98 | | await commandSender.SendAsync(command, CommandStrategy.Background, CreateHeaders(), CancellationToken.None); |
| | 1 | 99 | | return DispatchWorkflowResponse.Success(); |
| | 1 | 100 | | } |
| | | 101 | | |
| | | 102 | | private IDictionary<object, object> CreateHeaders() |
| | | 103 | | { |
| | 32 | 104 | | return TenantHeaders.CreateHeaders(tenantAccessor.Tenant?.Id); |
| | | 105 | | } |
| | | 106 | | } |