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