| | | 1 | | using Elsa.Common.DistributedHosting; |
| | | 2 | | using Elsa.Workflows.Helpers; |
| | | 3 | | using Elsa.Workflows.Runtime.Exceptions; |
| | | 4 | | using Elsa.Workflows.Runtime.Filters; |
| | | 5 | | using Elsa.Workflows.Runtime.Messages; |
| | | 6 | | using Elsa.Workflows.Runtime.Options; |
| | | 7 | | using Medallion.Threading; |
| | | 8 | | using Microsoft.Extensions.Logging; |
| | | 9 | | using Microsoft.Extensions.Options; |
| | | 10 | | |
| | | 11 | | namespace Elsa.Workflows.Runtime; |
| | | 12 | | |
| | | 13 | | /// <inheritdoc /> |
| | 105 | 14 | | public class WorkflowResumer( |
| | 105 | 15 | | IWorkflowRuntime workflowRuntime, |
| | 105 | 16 | | IBookmarkStore bookmarkStore, |
| | 105 | 17 | | IStimulusHasher stimulusHasher, |
| | 105 | 18 | | IDistributedLockProvider distributedLockProvider, |
| | 105 | 19 | | IOptions<DistributedLockingOptions> distributedLockingOptions, |
| | 105 | 20 | | ILogger<WorkflowResumer> logger) : IWorkflowResumer |
| | | 21 | | { |
| | | 22 | | /// <inheritdoc /> |
| | | 23 | | public Task<IEnumerable<RunWorkflowInstanceResponse>> ResumeAsync<TActivity>(object stimulus, ResumeBookmarkOptions? |
| | | 24 | | { |
| | 0 | 25 | | return ResumeAsync<TActivity>(stimulus, null, options, cancellationToken); |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <inheritdoc /> |
| | | 29 | | public async Task<IEnumerable<RunWorkflowInstanceResponse>> ResumeAsync<TActivity>(object stimulus, string? workflow |
| | | 30 | | { |
| | 0 | 31 | | var activityTypeName = ActivityTypeNameHelper.GenerateTypeName<TActivity>(); |
| | 0 | 32 | | var stimulusHash = stimulusHasher.Hash(activityTypeName, stimulus); |
| | 0 | 33 | | var bookmarkFilter = new BookmarkFilter |
| | 0 | 34 | | { |
| | 0 | 35 | | Name = activityTypeName, |
| | 0 | 36 | | WorkflowInstanceId = workflowInstanceId, |
| | 0 | 37 | | Hash = stimulusHash, |
| | 0 | 38 | | }; |
| | 0 | 39 | | return await ResumeAsync(bookmarkFilter, options, cancellationToken); |
| | 0 | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <inheritdoc /> |
| | | 43 | | public async Task<RunWorkflowInstanceResponse?> ResumeAsync(string bookmarkId, IDictionary<string, object> input, Ca |
| | | 44 | | { |
| | 0 | 45 | | var bookmarkFilter = new BookmarkFilter |
| | 0 | 46 | | { |
| | 0 | 47 | | BookmarkId = bookmarkId |
| | 0 | 48 | | }; |
| | 0 | 49 | | var options = new ResumeBookmarkOptions |
| | 0 | 50 | | { |
| | 0 | 51 | | Input = input |
| | 0 | 52 | | }; |
| | 0 | 53 | | var responses = await ResumeAsync(bookmarkFilter, options, cancellationToken); |
| | 0 | 54 | | return responses.FirstOrDefault(); |
| | 0 | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <inheritdoc /> |
| | | 58 | | public async Task<RunWorkflowInstanceResponse?> ResumeAsync<TActivity>(string bookmarkId, ResumeBookmarkOptions? opt |
| | | 59 | | { |
| | 0 | 60 | | var activityTypeName = ActivityTypeNameHelper.GenerateTypeName<TActivity>(); |
| | 0 | 61 | | var bookmarkFilter = new BookmarkFilter |
| | 0 | 62 | | { |
| | 0 | 63 | | Name = activityTypeName, |
| | 0 | 64 | | BookmarkId = bookmarkId |
| | 0 | 65 | | }; |
| | 0 | 66 | | var response = await ResumeAsync(bookmarkFilter, options, cancellationToken); |
| | 0 | 67 | | return response.FirstOrDefault(); |
| | 0 | 68 | | } |
| | | 69 | | |
| | | 70 | | public async Task<IEnumerable<RunWorkflowInstanceResponse>> ResumeAsync(ResumeBookmarkRequest request, CancellationT |
| | | 71 | | { |
| | 0 | 72 | | var filter = new BookmarkFilter |
| | 0 | 73 | | { |
| | 0 | 74 | | BookmarkId = request.BookmarkId, |
| | 0 | 75 | | ActivityInstanceId = request.ActivityInstanceId ?? request.ActivityHandle?.ActivityInstanceId, |
| | 0 | 76 | | }; |
| | | 77 | | |
| | 0 | 78 | | var resumeOptions = new ResumeBookmarkOptions() |
| | 0 | 79 | | { |
| | 0 | 80 | | Input = request.Input, |
| | 0 | 81 | | Properties = request.Properties, |
| | 0 | 82 | | }; |
| | 0 | 83 | | return await ResumeAsync(filter, resumeOptions, cancellationToken); |
| | 0 | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <inheritdoc /> |
| | | 87 | | public async Task<IEnumerable<RunWorkflowInstanceResponse>> ResumeAsync(BookmarkFilter filter, ResumeBookmarkOptions |
| | | 88 | | { |
| | 1106 | 89 | | var hashableFilterString = filter.GetHashableString(); |
| | 1106 | 90 | | var lockKey = $"workflow-resumer:{hashableFilterString}"; |
| | | 91 | | |
| | | 92 | | try |
| | | 93 | | { |
| | 1106 | 94 | | await using var filterLock = await distributedLockProvider.AcquireLockAsync(lockKey, distributedLockingOptio |
| | 1106 | 95 | | var bookmarks = (await bookmarkStore.FindManyAsync(filter, cancellationToken)).ToList(); |
| | | 96 | | |
| | 1106 | 97 | | if (bookmarks.Count == 0) |
| | | 98 | | { |
| | 1086 | 99 | | logger.LogDebug("No bookmarks found in store for filter {@Filter}", filter); |
| | 1086 | 100 | | return []; |
| | | 101 | | } |
| | | 102 | | |
| | 20 | 103 | | var responses = new List<RunWorkflowInstanceResponse>(); |
| | 80 | 104 | | foreach (var bookmark in bookmarks) |
| | | 105 | | { |
| | 20 | 106 | | var workflowClient = await workflowRuntime.CreateClientAsync(bookmark.WorkflowInstanceId, cancellationTo |
| | 20 | 107 | | var runRequest = new RunWorkflowInstanceRequest |
| | 20 | 108 | | { |
| | 20 | 109 | | Input = options?.Input, |
| | 20 | 110 | | Properties = options?.Properties, |
| | 20 | 111 | | BookmarkId = bookmark.Id |
| | 20 | 112 | | }; |
| | | 113 | | |
| | | 114 | | try |
| | | 115 | | { |
| | 20 | 116 | | var response = await workflowClient.RunInstanceAsync(runRequest, cancellationToken); |
| | 20 | 117 | | logger.LogDebug("Resumed workflow instance {WorkflowInstanceId} with bookmark {BookmarkId}", bookmar |
| | 20 | 118 | | responses.Add(response); |
| | 20 | 119 | | } |
| | 0 | 120 | | catch (WorkflowInstanceNotFoundException) |
| | | 121 | | { |
| | | 122 | | // The workflow instance does not (yet) exist in the DB. |
| | 0 | 123 | | logger.LogDebug("No workflow instance with ID {WorkflowInstanceId} found for bookmark {BookmarkId} a |
| | 0 | 124 | | } |
| | 20 | 125 | | } |
| | | 126 | | |
| | 20 | 127 | | return responses; |
| | 0 | 128 | | } |
| | 0 | 129 | | catch (TimeoutException e) |
| | | 130 | | { |
| | | 131 | | // Rethrow but with a more specific message. |
| | 0 | 132 | | throw new TimeoutException($"Could not acquire distributed lock with key '{lockKey}' within the configured t |
| | | 133 | | } |
| | 1106 | 134 | | } |
| | | 135 | | } |