| | | 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> |
| | 441 | 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 |
| | 24 | 21 | | await notificationSender.SendAsync(new WorkflowDefinitionDispatching(request), cancellationToken); |
| | | 22 | | |
| | 24 | 23 | | var command = new DispatchWorkflowDefinitionCommand(request.DefinitionVersionId) |
| | 24 | 24 | | { |
| | 24 | 25 | | Input = request.Input, |
| | 24 | 26 | | Properties = request.Properties, |
| | 24 | 27 | | CorrelationId = request.CorrelationId, |
| | 24 | 28 | | InstanceId = request.InstanceId, |
| | 24 | 29 | | TriggerActivityId = request.TriggerActivityId, |
| | 24 | 30 | | ParentWorkflowInstanceId = request.ParentWorkflowInstanceId, |
| | 24 | 31 | | }; |
| | | 32 | | |
| | 24 | 33 | | await commandSender.SendAsync(command, CommandStrategy.Background, CreateHeaders(), cancellationToken); |
| | 24 | 34 | | var response = DispatchWorkflowResponse.Success(); |
| | | 35 | | |
| | | 36 | | // Emit dispatched notification |
| | 24 | 37 | | await notificationSender.SendAsync(new WorkflowDefinitionDispatched(request, response), cancellationToken); |
| | | 38 | | |
| | 24 | 39 | | return response; |
| | 24 | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <inheritdoc /> |
| | | 43 | | public async Task<DispatchWorkflowResponse> DispatchAsync(DispatchWorkflowInstanceRequest request, DispatchWorkflowO |
| | | 44 | | { |
| | | 45 | | // Emit dispatching notification |
| | 1 | 46 | | await notificationSender.SendAsync(new WorkflowInstanceDispatching(request), cancellationToken); |
| | | 47 | | |
| | 1 | 48 | | var command = new DispatchWorkflowInstanceCommand(request.InstanceId){ |
| | 1 | 49 | | BookmarkId = request.BookmarkId, |
| | 1 | 50 | | ActivityHandle = request.ActivityHandle, |
| | 1 | 51 | | Input = request.Input, |
| | 1 | 52 | | Properties = request.Properties, |
| | 1 | 53 | | CorrelationId = request.CorrelationId}; |
| | | 54 | | |
| | 1 | 55 | | await commandSender.SendAsync(command, CommandStrategy.Background, CreateHeaders(), cancellationToken); |
| | 1 | 56 | | var response = DispatchWorkflowResponse.Success(); |
| | | 57 | | |
| | | 58 | | // Emit dispatched notification |
| | 1 | 59 | | await notificationSender.SendAsync(new WorkflowInstanceDispatched(request, response), cancellationToken); |
| | | 60 | | |
| | 1 | 61 | | return response; |
| | 1 | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <inheritdoc /> |
| | | 65 | | public async Task<DispatchWorkflowResponse> DispatchAsync(DispatchTriggerWorkflowsRequest request, DispatchWorkflowO |
| | | 66 | | { |
| | 0 | 67 | | var command = new DispatchTriggerWorkflowsCommand(request.ActivityTypeName, request.BookmarkPayload) |
| | 0 | 68 | | { |
| | 0 | 69 | | CorrelationId = request.CorrelationId, |
| | 0 | 70 | | WorkflowInstanceId = request.WorkflowInstanceId, |
| | 0 | 71 | | ActivityInstanceId = request.ActivityInstanceId, |
| | 0 | 72 | | Input = request.Input, |
| | 0 | 73 | | Properties = request.Properties |
| | 0 | 74 | | }; |
| | 0 | 75 | | await commandSender.SendAsync(command, CommandStrategy.Background, CreateHeaders(), cancellationToken); |
| | 0 | 76 | | return DispatchWorkflowResponse.Success(); |
| | 0 | 77 | | } |
| | | 78 | | |
| | | 79 | | /// <inheritdoc /> |
| | | 80 | | public async Task<DispatchWorkflowResponse> DispatchAsync(DispatchResumeWorkflowsRequest request, DispatchWorkflowOp |
| | | 81 | | { |
| | 0 | 82 | | var command = new DispatchResumeWorkflowsCommand(request.ActivityTypeName, request.BookmarkPayload) |
| | 0 | 83 | | { |
| | 0 | 84 | | CorrelationId = request.CorrelationId, |
| | 0 | 85 | | WorkflowInstanceId = request.WorkflowInstanceId, |
| | 0 | 86 | | ActivityInstanceId = request.ActivityInstanceId, |
| | 0 | 87 | | Input = request.Input |
| | 0 | 88 | | }; |
| | 0 | 89 | | await commandSender.SendAsync(command, CommandStrategy.Background, CreateHeaders(), cancellationToken); |
| | 0 | 90 | | return DispatchWorkflowResponse.Success(); |
| | 0 | 91 | | } |
| | | 92 | | |
| | | 93 | | private IDictionary<object, object> CreateHeaders() |
| | | 94 | | { |
| | 25 | 95 | | return TenantHeaders.CreateHeaders(tenantAccessor.Tenant?.Id); |
| | | 96 | | } |
| | | 97 | | } |