| | | 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> |
| | 476 | 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); |
| | 28 | 22 | | var command = WorkflowDispatchCommandFactory.CreateCommand(request); |
| | | 23 | | |
| | | 24 | | // Background commands run independently of caller's lifecycle. |
| | 28 | 25 | | await commandSender.SendAsync(command, CommandStrategy.Background, CreateHeaders(), CancellationToken.None); |
| | 28 | 26 | | var response = DispatchWorkflowResponse.Success(); |
| | | 27 | | |
| | | 28 | | // Emit dispatched notification |
| | 28 | 29 | | await notificationSender.SendAsync(new WorkflowDefinitionDispatched(request, response), cancellationToken); |
| | | 30 | | |
| | 28 | 31 | | return response; |
| | 28 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <inheritdoc /> |
| | | 35 | | public async Task<DispatchWorkflowResponse> DispatchAsync(DispatchWorkflowInstanceRequest request, DispatchWorkflowO |
| | | 36 | | { |
| | | 37 | | // Emit dispatching notification |
| | 2 | 38 | | await notificationSender.SendAsync(new WorkflowInstanceDispatching(request), cancellationToken); |
| | 2 | 39 | | var command = WorkflowDispatchCommandFactory.CreateCommand(request); |
| | | 40 | | |
| | | 41 | | // Background commands run independently of caller's lifecycle. |
| | 2 | 42 | | await commandSender.SendAsync(command, CommandStrategy.Background, CreateHeaders(), CancellationToken.None); |
| | 2 | 43 | | var response = DispatchWorkflowResponse.Success(); |
| | | 44 | | |
| | | 45 | | // Emit dispatched notification |
| | 2 | 46 | | await notificationSender.SendAsync(new WorkflowInstanceDispatched(request, response), cancellationToken); |
| | | 47 | | |
| | 2 | 48 | | return response; |
| | 2 | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <inheritdoc /> |
| | | 52 | | public async Task<DispatchWorkflowResponse> DispatchAsync(DispatchTriggerWorkflowsRequest request, DispatchWorkflowO |
| | | 53 | | { |
| | 1 | 54 | | var command = WorkflowDispatchCommandFactory.CreateCommand(request); |
| | | 55 | | |
| | | 56 | | // Background commands run independently of caller's lifecycle. |
| | 1 | 57 | | await commandSender.SendAsync(command, CommandStrategy.Background, CreateHeaders(), CancellationToken.None); |
| | 1 | 58 | | return DispatchWorkflowResponse.Success(); |
| | 1 | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <inheritdoc /> |
| | | 62 | | public async Task<DispatchWorkflowResponse> DispatchAsync(DispatchResumeWorkflowsRequest request, DispatchWorkflowOp |
| | | 63 | | { |
| | 1 | 64 | | var command = WorkflowDispatchCommandFactory.CreateCommand(request); |
| | | 65 | | |
| | | 66 | | // Background commands run independently of caller's lifecycle. |
| | 1 | 67 | | await commandSender.SendAsync(command, CommandStrategy.Background, CreateHeaders(), CancellationToken.None); |
| | 1 | 68 | | return DispatchWorkflowResponse.Success(); |
| | 1 | 69 | | } |
| | | 70 | | |
| | | 71 | | private IDictionary<object, object> CreateHeaders() |
| | | 72 | | { |
| | 32 | 73 | | return TenantHeaders.CreateHeaders(tenantAccessor.Tenant?.Id); |
| | | 74 | | } |
| | | 75 | | } |