| | | 1 | | using Elsa.Mediator.Contracts; |
| | | 2 | | using Elsa.Workflows.Helpers; |
| | | 3 | | using Elsa.Workflows.Notifications; |
| | | 4 | | using Elsa.Workflows.Runtime.Activities; |
| | | 5 | | using Elsa.Workflows.Runtime.Messages; |
| | | 6 | | using Elsa.Workflows.Runtime.Notifications; |
| | | 7 | | using Elsa.Workflows.Runtime.Stimuli; |
| | | 8 | | using JetBrains.Annotations; |
| | | 9 | | |
| | | 10 | | namespace Elsa.Workflows.Runtime.Handlers; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Resumes any blocking <see cref="DispatchWorkflow"/> activities when its child workflow completes. |
| | | 14 | | /// </summary> |
| | | 15 | | [PublicAPI] |
| | 734 | 16 | | internal class ResumeDispatchWorkflowActivity(IBookmarkQueue bookmarkQueue, IStimulusHasher stimulusHasher, IStimulusSen |
| | | 17 | | INotificationHandler<WorkflowExecuted>, |
| | | 18 | | INotificationHandler<DispatchWorkflowActivationDenied> |
| | | 19 | | { |
| | 3 | 20 | | private static readonly string ActivityTypeName = ActivityTypeNameHelper.GenerateTypeName<DispatchWorkflow>(); |
| | | 21 | | |
| | | 22 | | public async Task HandleAsync(WorkflowExecuted notification, CancellationToken cancellationToken) |
| | | 23 | | { |
| | 605 | 24 | | var workflowState = notification.WorkflowState; |
| | | 25 | | |
| | 605 | 26 | | if (workflowState.Status != WorkflowStatus.Finished) |
| | 96 | 27 | | return; |
| | | 28 | | |
| | 509 | 29 | | var props = workflowState.Properties; |
| | 509 | 30 | | var waitForCompletion = props.TryGetValue("WaitForCompletion", out var waitForCompletionValue) && waitForComplet |
| | | 31 | | |
| | 509 | 32 | | if (!waitForCompletion) |
| | 490 | 33 | | return; |
| | | 34 | | |
| | 19 | 35 | | var parentInstanceId = props.TryGetValue("ParentInstanceId", out var parentInstanceIdValue) ? parentInstanceIdVa |
| | 19 | 36 | | if (string.IsNullOrWhiteSpace(parentInstanceId)) |
| | 0 | 37 | | return; |
| | | 38 | | |
| | 19 | 39 | | await EnqueueResumeAsync(parentInstanceId, workflowState.Id, workflowState.Output, cancellationToken); |
| | 605 | 40 | | } |
| | | 41 | | |
| | | 42 | | public async Task HandleAsync(DispatchWorkflowActivationDenied notification, CancellationToken cancellationToken) |
| | | 43 | | { |
| | 4 | 44 | | if (notification.ActivityTypeName != ActivityTypeName) |
| | 1 | 45 | | return; |
| | | 46 | | |
| | 3 | 47 | | var input = new Dictionary<string, object> |
| | 3 | 48 | | { |
| | 3 | 49 | | ["CannotStart"] = true, |
| | 3 | 50 | | ["WorkflowInstanceId"] = notification.WorkflowInstanceId |
| | 3 | 51 | | }; |
| | 3 | 52 | | await stimulusSender.SendAsync(notification.StimulusHash, new() |
| | 3 | 53 | | { |
| | 3 | 54 | | WorkflowInstanceId = notification.ParentWorkflowInstanceId, |
| | 3 | 55 | | Input = input |
| | 3 | 56 | | }, cancellationToken); |
| | 4 | 57 | | } |
| | | 58 | | |
| | | 59 | | private async Task EnqueueResumeAsync(string parentInstanceId, string workflowInstanceId, IDictionary<string, object |
| | | 60 | | { |
| | 19 | 61 | | var activityTypeName = ActivityTypeName; |
| | 19 | 62 | | var stimulus = new DispatchWorkflowStimulus(workflowInstanceId); |
| | 19 | 63 | | var stimulusHash = stimulusHasher.Hash(activityTypeName, stimulus); |
| | 19 | 64 | | await EnqueueResumeAsync(parentInstanceId, activityTypeName, stimulusHash, input, cancellationToken); |
| | 19 | 65 | | } |
| | | 66 | | |
| | | 67 | | private Task EnqueueResumeAsync(string parentInstanceId, string activityTypeName, string stimulusHash, IDictionary<s |
| | | 68 | | { |
| | 19 | 69 | | var bookmarkQueueItem = new NewBookmarkQueueItem |
| | 19 | 70 | | { |
| | 19 | 71 | | WorkflowInstanceId = parentInstanceId, |
| | 19 | 72 | | ActivityTypeName = activityTypeName, |
| | 19 | 73 | | StimulusHash = stimulusHash, |
| | 19 | 74 | | Options = new() |
| | 19 | 75 | | { |
| | 19 | 76 | | Input = input |
| | 19 | 77 | | } |
| | 19 | 78 | | }; |
| | | 79 | | |
| | 19 | 80 | | return bookmarkQueue.EnqueueAsync(bookmarkQueueItem, cancellationToken); |
| | | 81 | | } |
| | | 82 | | } |