| | | 1 | | using Elsa.Mediator.Contracts; |
| | | 2 | | using Elsa.Mediator.Models; |
| | | 3 | | using Elsa.Workflows.Models; |
| | | 4 | | using Elsa.Workflows.Runtime.Commands; |
| | | 5 | | using Elsa.Workflows.Runtime.Messages; |
| | | 6 | | using Elsa.Workflows.Runtime.Notifications; |
| | | 7 | | using JetBrains.Annotations; |
| | | 8 | | |
| | | 9 | | namespace Elsa.Workflows.Runtime.Handlers; |
| | | 10 | | |
| | | 11 | | // ReSharper disable once UnusedType.Global |
| | | 12 | | [UsedImplicitly] |
| | 90 | 13 | | internal class DispatchWorkflowCommandHandler(IStimulusSender stimulusSender, IWorkflowRuntime workflowRuntime, INotific |
| | | 14 | | ICommandHandler<DispatchTriggerWorkflowsCommand>, |
| | | 15 | | ICommandHandler<DispatchWorkflowDefinitionCommand>, |
| | | 16 | | ICommandHandler<DispatchWorkflowInstanceCommand>, |
| | | 17 | | ICommandHandler<DispatchResumeWorkflowsCommand> |
| | | 18 | | { |
| | | 19 | | public virtual async Task<Unit> HandleAsync(DispatchTriggerWorkflowsCommand command, CancellationToken cancellationT |
| | | 20 | | { |
| | 7 | 21 | | var activityTypeName = command.ActivityTypeName; |
| | 7 | 22 | | var stimulus = command.Stimulus; |
| | 7 | 23 | | var metadata = new StimulusMetadata |
| | 7 | 24 | | { |
| | 7 | 25 | | CorrelationId = command.CorrelationId, |
| | 7 | 26 | | ActivityInstanceId = command.ActivityInstanceId, |
| | 7 | 27 | | WorkflowInstanceId = command.WorkflowInstanceId, |
| | 7 | 28 | | Input = command.Input, |
| | 7 | 29 | | Properties = command.Properties, |
| | 7 | 30 | | }; |
| | 7 | 31 | | await stimulusSender.SendAsync(activityTypeName, stimulus, metadata, cancellationToken); |
| | | 32 | | |
| | 7 | 33 | | return Unit.Instance; |
| | 7 | 34 | | } |
| | | 35 | | |
| | | 36 | | public virtual async Task<Unit> HandleAsync(DispatchWorkflowDefinitionCommand command, CancellationToken cancellatio |
| | | 37 | | { |
| | 43 | 38 | | var client = await workflowRuntime.CreateClientAsync(command.InstanceId, cancellationToken); |
| | | 39 | | |
| | 43 | 40 | | if (command.SkipIfInstanceExists && !string.IsNullOrWhiteSpace(command.InstanceId) && await client.InstanceExist |
| | 1 | 41 | | return Unit.Instance; |
| | | 42 | | |
| | 42 | 43 | | var properties = DispatchWorkflowActivationDeniedRoute.SanitizeAndRead(command.Properties, out var denialRoute); |
| | | 44 | | |
| | 42 | 45 | | var createRequest = new CreateAndRunWorkflowInstanceRequest |
| | 42 | 46 | | { |
| | 42 | 47 | | WorkflowDefinitionHandle = WorkflowDefinitionHandle.ByDefinitionVersionId(command.DefinitionVersionId), |
| | 42 | 48 | | CorrelationId = command.CorrelationId, |
| | 42 | 49 | | Input = command.Input, |
| | 42 | 50 | | Properties = properties, |
| | 42 | 51 | | ParentId = command.ParentWorkflowInstanceId, |
| | 42 | 52 | | TriggerActivityId = command.TriggerActivityId, |
| | 42 | 53 | | SchedulingActivityExecutionId = command.SchedulingActivityExecutionId, |
| | 42 | 54 | | SchedulingWorkflowInstanceId = command.SchedulingWorkflowInstanceId, |
| | 42 | 55 | | SchedulingCallStackDepth = command.SchedulingCallStackDepth |
| | 42 | 56 | | }; |
| | 42 | 57 | | var response = await client.CreateAndRunInstanceAsync(createRequest, cancellationToken); |
| | 41 | 58 | | if (response.CannotStart && |
| | 41 | 59 | | denialRoute is { } route && |
| | 41 | 60 | | !string.IsNullOrWhiteSpace(command.ParentWorkflowInstanceId) && |
| | 41 | 61 | | !string.IsNullOrWhiteSpace(command.InstanceId)) |
| | | 62 | | { |
| | 3 | 63 | | await notificationSender.SendAsync(new DispatchWorkflowActivationDenied(command.ParentWorkflowInstanceId, co |
| | | 64 | | } |
| | | 65 | | |
| | 41 | 66 | | return Unit.Instance; |
| | 42 | 67 | | } |
| | | 68 | | |
| | | 69 | | public virtual async Task<Unit> HandleAsync(DispatchWorkflowInstanceCommand command, CancellationToken cancellationT |
| | | 70 | | { |
| | 1 | 71 | | var runRequest = new RunWorkflowInstanceRequest |
| | 1 | 72 | | { |
| | 1 | 73 | | BookmarkId = command.BookmarkId, |
| | 1 | 74 | | ActivityHandle = command.ActivityHandle, |
| | 1 | 75 | | Input = command.Input, |
| | 1 | 76 | | Properties = command.Properties |
| | 1 | 77 | | }; |
| | 1 | 78 | | var client = await workflowRuntime.CreateClientAsync(command.InstanceId, cancellationToken); |
| | 1 | 79 | | await client.RunInstanceAsync(runRequest, cancellationToken); |
| | | 80 | | |
| | 1 | 81 | | return Unit.Instance; |
| | 1 | 82 | | } |
| | | 83 | | |
| | | 84 | | public virtual async Task<Unit> HandleAsync(DispatchResumeWorkflowsCommand command, CancellationToken cancellationTo |
| | | 85 | | { |
| | 0 | 86 | | var activityTypeName = command.ActivityTypeName; |
| | 0 | 87 | | var stimulus = command.Stimulus; |
| | 0 | 88 | | var metadata = new StimulusMetadata |
| | 0 | 89 | | { |
| | 0 | 90 | | CorrelationId = command.CorrelationId, |
| | 0 | 91 | | WorkflowInstanceId = command.WorkflowInstanceId, |
| | 0 | 92 | | ActivityInstanceId = command.ActivityInstanceId, |
| | 0 | 93 | | Properties = command.Properties, |
| | 0 | 94 | | Input = command.Input |
| | 0 | 95 | | }; |
| | 0 | 96 | | await stimulusSender.SendAsync(activityTypeName, stimulus, metadata, cancellationToken); |
| | | 97 | | |
| | 0 | 98 | | return Unit.Instance; |
| | 0 | 99 | | } |
| | | 100 | | } |