| | | 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] |
| | 64 | 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 | | { |
| | 29 | 37 | | var client = await workflowRuntime.CreateClientAsync(command.InstanceId, cancellationToken); |
| | | 38 | | |
| | 29 | 39 | | if (command.SkipIfInstanceExists && !string.IsNullOrWhiteSpace(command.InstanceId) && await client.InstanceExist |
| | 1 | 40 | | return Unit.Instance; |
| | | 41 | | |
| | 28 | 42 | | var createRequest = new CreateAndRunWorkflowInstanceRequest |
| | 28 | 43 | | { |
| | 28 | 44 | | WorkflowDefinitionHandle = WorkflowDefinitionHandle.ByDefinitionVersionId(command.DefinitionVersionId), |
| | 28 | 45 | | CorrelationId = command.CorrelationId, |
| | 28 | 46 | | Input = command.Input, |
| | 28 | 47 | | Properties = command.Properties, |
| | 28 | 48 | | ParentId = command.ParentWorkflowInstanceId, |
| | 28 | 49 | | TriggerActivityId = command.TriggerActivityId, |
| | 28 | 50 | | SchedulingActivityExecutionId = command.SchedulingActivityExecutionId, |
| | 28 | 51 | | SchedulingWorkflowInstanceId = command.SchedulingWorkflowInstanceId, |
| | 28 | 52 | | SchedulingCallStackDepth = command.SchedulingCallStackDepth |
| | 28 | 53 | | }; |
| | 28 | 54 | | await client.CreateAndRunInstanceAsync(createRequest, cancellationToken); |
| | 28 | 55 | | return Unit.Instance; |
| | 29 | 56 | | } |
| | | 57 | | |
| | | 58 | | public virtual async Task<Unit> HandleAsync(DispatchWorkflowInstanceCommand command, CancellationToken cancellationT |
| | | 59 | | { |
| | 0 | 60 | | var runRequest = new RunWorkflowInstanceRequest |
| | 0 | 61 | | { |
| | 0 | 62 | | BookmarkId = command.BookmarkId, |
| | 0 | 63 | | ActivityHandle = command.ActivityHandle, |
| | 0 | 64 | | Input = command.Input, |
| | 0 | 65 | | Properties = command.Properties |
| | 0 | 66 | | }; |
| | 0 | 67 | | var client = await workflowRuntime.CreateClientAsync(command.InstanceId, cancellationToken); |
| | 0 | 68 | | await client.RunInstanceAsync(runRequest, cancellationToken); |
| | | 69 | | |
| | 0 | 70 | | return Unit.Instance; |
| | 0 | 71 | | } |
| | | 72 | | |
| | | 73 | | public virtual async Task<Unit> HandleAsync(DispatchResumeWorkflowsCommand command, CancellationToken cancellationTo |
| | | 74 | | { |
| | 0 | 75 | | var activityTypeName = command.ActivityTypeName; |
| | 0 | 76 | | var stimulus = command.Stimulus; |
| | 0 | 77 | | var metadata = new StimulusMetadata |
| | 0 | 78 | | { |
| | 0 | 79 | | CorrelationId = command.CorrelationId, |
| | 0 | 80 | | WorkflowInstanceId = command.WorkflowInstanceId, |
| | 0 | 81 | | ActivityInstanceId = command.ActivityInstanceId, |
| | 0 | 82 | | Properties = command.Properties, |
| | 0 | 83 | | Input = command.Input |
| | 0 | 84 | | }; |
| | 0 | 85 | | await stimulusSender.SendAsync(activityTypeName, stimulus, metadata, cancellationToken); |
| | | 86 | | |
| | 0 | 87 | | return Unit.Instance; |
| | 0 | 88 | | } |
| | | 89 | | } |