| | | 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 JetBrains.Annotations; |
| | | 7 | | |
| | | 8 | | namespace Elsa.Workflows.Runtime.Handlers; |
| | | 9 | | |
| | | 10 | | // ReSharper disable once UnusedType.Global |
| | | 11 | | [UsedImplicitly] |
| | 53 | 12 | | internal class DispatchWorkflowCommandHandler(IStimulusSender stimulusSender, IWorkflowRuntime workflowRuntime) : |
| | | 13 | | ICommandHandler<DispatchTriggerWorkflowsCommand>, |
| | | 14 | | ICommandHandler<DispatchWorkflowDefinitionCommand>, |
| | | 15 | | ICommandHandler<DispatchWorkflowInstanceCommand>, |
| | | 16 | | ICommandHandler<DispatchResumeWorkflowsCommand> |
| | | 17 | | { |
| | | 18 | | public virtual async Task<Unit> HandleAsync(DispatchTriggerWorkflowsCommand command, CancellationToken cancellationT |
| | | 19 | | { |
| | 0 | 20 | | var activityTypeName = command.ActivityTypeName; |
| | 0 | 21 | | var stimulus = command.Stimulus; |
| | 0 | 22 | | var metadata = new StimulusMetadata |
| | 0 | 23 | | { |
| | 0 | 24 | | CorrelationId = command.CorrelationId, |
| | 0 | 25 | | ActivityInstanceId = command.ActivityInstanceId, |
| | 0 | 26 | | WorkflowInstanceId = command.WorkflowInstanceId, |
| | 0 | 27 | | Input = command.Input, |
| | 0 | 28 | | Properties = command.Properties, |
| | 0 | 29 | | }; |
| | 0 | 30 | | await stimulusSender.SendAsync(activityTypeName, stimulus, metadata, cancellationToken); |
| | | 31 | | |
| | 0 | 32 | | return Unit.Instance; |
| | 0 | 33 | | } |
| | | 34 | | |
| | | 35 | | public virtual async Task<Unit> HandleAsync(DispatchWorkflowDefinitionCommand command, CancellationToken cancellatio |
| | | 36 | | { |
| | 23 | 37 | | var client = await workflowRuntime.CreateClientAsync(command.InstanceId, cancellationToken); |
| | 23 | 38 | | var createRequest = new CreateAndRunWorkflowInstanceRequest |
| | 23 | 39 | | { |
| | 23 | 40 | | WorkflowDefinitionHandle = WorkflowDefinitionHandle.ByDefinitionVersionId(command.DefinitionVersionId), |
| | 23 | 41 | | CorrelationId = command.CorrelationId, |
| | 23 | 42 | | Input = command.Input, |
| | 23 | 43 | | Properties = command.Properties, |
| | 23 | 44 | | ParentId = command.ParentWorkflowInstanceId, |
| | 23 | 45 | | TriggerActivityId = command.TriggerActivityId |
| | 23 | 46 | | }; |
| | 23 | 47 | | await client.CreateAndRunInstanceAsync(createRequest, cancellationToken); |
| | 23 | 48 | | return Unit.Instance; |
| | 23 | 49 | | } |
| | | 50 | | |
| | | 51 | | public virtual async Task<Unit> HandleAsync(DispatchWorkflowInstanceCommand command, CancellationToken cancellationT |
| | | 52 | | { |
| | 0 | 53 | | var runRequest = new RunWorkflowInstanceRequest |
| | 0 | 54 | | { |
| | 0 | 55 | | BookmarkId = command.BookmarkId, |
| | 0 | 56 | | ActivityHandle = command.ActivityHandle, |
| | 0 | 57 | | Input = command.Input, |
| | 0 | 58 | | Properties = command.Properties |
| | 0 | 59 | | }; |
| | 0 | 60 | | var client = await workflowRuntime.CreateClientAsync(command.InstanceId, cancellationToken); |
| | 0 | 61 | | await client.RunInstanceAsync(runRequest, cancellationToken); |
| | | 62 | | |
| | 0 | 63 | | return Unit.Instance; |
| | 0 | 64 | | } |
| | | 65 | | |
| | | 66 | | public virtual async Task<Unit> HandleAsync(DispatchResumeWorkflowsCommand command, CancellationToken cancellationTo |
| | | 67 | | { |
| | 0 | 68 | | var activityTypeName = command.ActivityTypeName; |
| | 0 | 69 | | var stimulus = command.Stimulus; |
| | 0 | 70 | | var metadata = new StimulusMetadata |
| | 0 | 71 | | { |
| | 0 | 72 | | CorrelationId = command.CorrelationId, |
| | 0 | 73 | | WorkflowInstanceId = command.WorkflowInstanceId, |
| | 0 | 74 | | ActivityInstanceId = command.ActivityInstanceId, |
| | 0 | 75 | | Properties = command.Properties, |
| | 0 | 76 | | Input = command.Input |
| | 0 | 77 | | }; |
| | 0 | 78 | | await stimulusSender.SendAsync(activityTypeName, stimulus, metadata, cancellationToken); |
| | | 79 | | |
| | 0 | 80 | | return Unit.Instance; |
| | 0 | 81 | | } |
| | | 82 | | } |