| | | 1 | | using Elsa.Workflows.Runtime.Filters; |
| | | 2 | | using Elsa.Workflows.Runtime.Messages; |
| | | 3 | | using Elsa.Workflows.Runtime.Results; |
| | | 4 | | using Microsoft.Extensions.Logging; |
| | | 5 | | using Open.Linq.AsyncExtensions; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Workflows.Runtime; |
| | | 8 | | |
| | | 9 | | /// <inheritdoc /> |
| | 57 | 10 | | public class StimulusSender( |
| | 57 | 11 | | IStimulusHasher stimulusHasher, |
| | 57 | 12 | | ITriggerBoundWorkflowService triggerBoundWorkflowService, |
| | 57 | 13 | | IWorkflowResumer workflowResumer, |
| | 57 | 14 | | IBookmarkQueue bookmarkQueue, |
| | 57 | 15 | | ITriggerInvoker triggerInvoker, |
| | 57 | 16 | | ILogger<StimulusSender> logger) : IStimulusSender |
| | | 17 | | { |
| | | 18 | | /// <inheritdoc /> |
| | | 19 | | public Task<SendStimulusResult> SendAsync(string activityTypeName, object stimulus, StimulusMetadata? metadata = nul |
| | | 20 | | { |
| | 6 | 21 | | var stimulusHash = stimulusHasher.Hash(activityTypeName, stimulus, metadata?.ActivityInstanceId); |
| | 6 | 22 | | return SendAsync(stimulusHash, metadata, cancellationToken); |
| | | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <inheritdoc /> |
| | | 26 | | public async Task<SendStimulusResult> SendAsync(string stimulusHash, StimulusMetadata? metadata = null, Cancellation |
| | | 27 | | { |
| | 6 | 28 | | var responses = new List<RunWorkflowInstanceResponse>(); |
| | | 29 | | |
| | 6 | 30 | | if (metadata == null || (metadata.WorkflowInstanceId == null && metadata.BookmarkId == null && metadata.Activity |
| | | 31 | | { |
| | 5 | 32 | | var triggered = await TriggerNewWorkflowsAsync(stimulusHash, metadata, cancellationToken); |
| | 5 | 33 | | responses.AddRange(triggered); |
| | | 34 | | } |
| | | 35 | | |
| | 6 | 36 | | var resumed = await ResumeExistingWorkflowsAsync(stimulusHash, metadata, cancellationToken); |
| | 6 | 37 | | responses.AddRange(resumed); |
| | 6 | 38 | | return new(responses); |
| | 6 | 39 | | } |
| | | 40 | | |
| | | 41 | | private async Task<ICollection<RunWorkflowInstanceResponse>> TriggerNewWorkflowsAsync(string stimulusHash, StimulusM |
| | | 42 | | { |
| | 5 | 43 | | var triggerBoundWorkflows = await triggerBoundWorkflowService.FindManyAsync(stimulusHash, cancellationToken).ToL |
| | 5 | 44 | | var correlationId = metadata?.CorrelationId; |
| | 5 | 45 | | var input = metadata?.Input; |
| | 5 | 46 | | var properties = metadata?.Properties; |
| | 5 | 47 | | var parentId = metadata?.ParentWorkflowInstanceId; |
| | 5 | 48 | | var responses = new List<RunWorkflowInstanceResponse>(); |
| | | 49 | | |
| | 20 | 50 | | foreach (var triggerBoundWorkflow in triggerBoundWorkflows) |
| | | 51 | | { |
| | 5 | 52 | | var workflowGraph = triggerBoundWorkflow.WorkflowGraph; |
| | 5 | 53 | | var workflow = workflowGraph.Workflow; |
| | | 54 | | |
| | 20 | 55 | | foreach (var trigger in triggerBoundWorkflow.Triggers) |
| | | 56 | | { |
| | 5 | 57 | | var triggerRequest = new InvokeTriggerRequest |
| | 5 | 58 | | { |
| | 5 | 59 | | CorrelationId = correlationId, |
| | 5 | 60 | | Workflow = workflow, |
| | 5 | 61 | | ActivityId = trigger.ActivityId, |
| | 5 | 62 | | Input = input, |
| | 5 | 63 | | Properties = properties, |
| | 5 | 64 | | ParentWorkflowInstanceId = parentId |
| | 5 | 65 | | }; |
| | | 66 | | |
| | 5 | 67 | | var response = await triggerInvoker.InvokeAsync(triggerRequest, cancellationToken); |
| | | 68 | | |
| | 5 | 69 | | if (response.CannotStart) |
| | | 70 | | { |
| | 0 | 71 | | logger.LogWarning("Workflow activation strategy disallowed starting workflow {WorkflowDefinitionHand |
| | 0 | 72 | | continue; |
| | | 73 | | } |
| | | 74 | | |
| | 5 | 75 | | responses.Add(response.ToRunWorkflowInstanceResponse()); |
| | | 76 | | } |
| | 5 | 77 | | } |
| | | 78 | | |
| | 5 | 79 | | return responses; |
| | 5 | 80 | | } |
| | | 81 | | |
| | | 82 | | private async Task<ICollection<RunWorkflowInstanceResponse>> ResumeExistingWorkflowsAsync(string stimulusHash, Stimu |
| | | 83 | | { |
| | 6 | 84 | | var input = metadata?.Input; |
| | 6 | 85 | | var properties = metadata?.Properties; |
| | | 86 | | |
| | 6 | 87 | | var bookmarkFilter = new BookmarkFilter |
| | 6 | 88 | | { |
| | 6 | 89 | | Hash = stimulusHash, |
| | 6 | 90 | | CorrelationId = metadata?.CorrelationId, |
| | 6 | 91 | | WorkflowInstanceId = metadata?.WorkflowInstanceId, |
| | 6 | 92 | | ActivityInstanceId = metadata?.ActivityInstanceId, |
| | 6 | 93 | | BookmarkId = metadata?.BookmarkId |
| | 6 | 94 | | }; |
| | 6 | 95 | | var responses = (await workflowResumer.ResumeAsync(bookmarkFilter, new() |
| | 6 | 96 | | { |
| | 6 | 97 | | Input = input, |
| | 6 | 98 | | Properties = properties |
| | 6 | 99 | | }, cancellationToken)).ToList(); |
| | | 100 | | |
| | 6 | 101 | | if (responses.Count > 0) |
| | | 102 | | { |
| | 2 | 103 | | logger.LogDebug("Successfully resumed {WorkflowCount} workflow instances using stimulus {StimulusHash}", res |
| | 2 | 104 | | return responses; |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | // If no bookmarks were matched, enqueue the request in case a matching bookmark is created in the near future. |
| | 4 | 108 | | var workflowInstanceId = metadata?.WorkflowInstanceId; |
| | | 109 | | |
| | 4 | 110 | | var bookmarkQueueItem = new NewBookmarkQueueItem |
| | 4 | 111 | | { |
| | 4 | 112 | | WorkflowInstanceId = workflowInstanceId, |
| | 4 | 113 | | BookmarkId = metadata?.BookmarkId, |
| | 4 | 114 | | CorrelationId = metadata?.CorrelationId, |
| | 4 | 115 | | StimulusHash = stimulusHash, |
| | 4 | 116 | | Options = new() |
| | 4 | 117 | | { |
| | 4 | 118 | | Input = input, |
| | 4 | 119 | | Properties = properties |
| | 4 | 120 | | } |
| | 4 | 121 | | }; |
| | | 122 | | |
| | 4 | 123 | | logger.LogDebug("Bookmark queue item enqueued with stimulus: {StimulusHash}", bookmarkQueueItem.StimulusHash); |
| | | 124 | | |
| | 4 | 125 | | await bookmarkQueue.EnqueueAsync(bookmarkQueueItem, cancellationToken); |
| | | 126 | | |
| | 4 | 127 | | return responses; |
| | 6 | 128 | | } |
| | | 129 | | } |