| | | 1 | | using Elsa.Workflows.Helpers; |
| | | 2 | | using Elsa.Workflows.Runtime.Exceptions; |
| | | 3 | | using Elsa.Workflows.Runtime.Filters; |
| | | 4 | | using Elsa.Workflows.Runtime.Messages; |
| | | 5 | | using Elsa.Workflows.Runtime.Options; |
| | | 6 | | using Microsoft.Extensions.Logging; |
| | | 7 | | |
| | | 8 | | namespace Elsa.Workflows.Runtime; |
| | | 9 | | |
| | | 10 | | /// <inheritdoc /> |
| | | 11 | | [Obsolete("Use WorkflowResumer instead.")] |
| | 0 | 12 | | public class BookmarkResumer(IWorkflowRuntime workflowRuntime, IBookmarkStore bookmarkStore, IStimulusHasher stimulusHas |
| | | 13 | | { |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public Task<ResumeBookmarkResult> ResumeAsync<TActivity>(object stimulus, ResumeBookmarkOptions? options = null, Can |
| | | 16 | | { |
| | 0 | 17 | | return ResumeAsync<TActivity>(stimulus, null, options, cancellationToken); |
| | | 18 | | } |
| | | 19 | | |
| | | 20 | | /// <inheritdoc /> |
| | | 21 | | public async Task<ResumeBookmarkResult> ResumeAsync<TActivity>(object stimulus, string? workflowInstanceId = null, R |
| | | 22 | | { |
| | 0 | 23 | | var activityTypeName = ActivityTypeNameHelper.GenerateTypeName<TActivity>(); |
| | 0 | 24 | | var stimulusHash = stimulusHasher.Hash(activityTypeName, stimulus); |
| | 0 | 25 | | var bookmarkFilter = new BookmarkFilter |
| | 0 | 26 | | { |
| | 0 | 27 | | Name = activityTypeName, |
| | 0 | 28 | | WorkflowInstanceId = workflowInstanceId, |
| | 0 | 29 | | Hash = stimulusHash, |
| | 0 | 30 | | }; |
| | 0 | 31 | | return await ResumeAsync(bookmarkFilter, options, cancellationToken); |
| | 0 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <inheritdoc /> |
| | | 35 | | public async Task<ResumeBookmarkResult> ResumeAsync(string bookmarkId, IDictionary<string, object> input, Cancellati |
| | | 36 | | { |
| | 0 | 37 | | var bookmarkFilter = new BookmarkFilter |
| | 0 | 38 | | { |
| | 0 | 39 | | BookmarkId = bookmarkId |
| | 0 | 40 | | }; |
| | 0 | 41 | | var options = new ResumeBookmarkOptions |
| | 0 | 42 | | { |
| | 0 | 43 | | Input = input |
| | 0 | 44 | | }; |
| | 0 | 45 | | return await ResumeAsync(bookmarkFilter, options, cancellationToken); |
| | 0 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <inheritdoc /> |
| | | 49 | | public async Task<ResumeBookmarkResult> ResumeAsync<TActivity>(string bookmarkId, ResumeBookmarkOptions? options = n |
| | | 50 | | { |
| | 0 | 51 | | var activityTypeName = ActivityTypeNameHelper.GenerateTypeName<TActivity>(); |
| | 0 | 52 | | var bookmarkFilter = new BookmarkFilter |
| | 0 | 53 | | { |
| | 0 | 54 | | Name = activityTypeName, |
| | 0 | 55 | | BookmarkId = bookmarkId |
| | 0 | 56 | | }; |
| | 0 | 57 | | return await ResumeAsync(bookmarkFilter, options, cancellationToken); |
| | 0 | 58 | | } |
| | | 59 | | |
| | | 60 | | public async Task<ResumeBookmarkResult> ResumeAsync(ResumeBookmarkRequest request, CancellationToken cancellationTok |
| | | 61 | | { |
| | 0 | 62 | | var runRequest = new RunWorkflowInstanceRequest |
| | 0 | 63 | | { |
| | 0 | 64 | | Input = request.Input, |
| | 0 | 65 | | Properties = request.Properties, |
| | 0 | 66 | | ActivityHandle = request.ActivityHandle, |
| | 0 | 67 | | BookmarkId = request.BookmarkId |
| | 0 | 68 | | }; |
| | | 69 | | |
| | 0 | 70 | | var workflowInstanceId = request.WorkflowInstanceId; |
| | 0 | 71 | | var workflowClient = await workflowRuntime.CreateClientAsync(workflowInstanceId, cancellationToken); |
| | 0 | 72 | | var response = await workflowClient.RunInstanceAsync(runRequest, cancellationToken); |
| | 0 | 73 | | return ResumeBookmarkResult.Found(response); |
| | 0 | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <inheritdoc /> |
| | | 77 | | public async Task<ResumeBookmarkResult> ResumeAsync(BookmarkFilter filter, ResumeBookmarkOptions? options = null, Ca |
| | | 78 | | { |
| | 0 | 79 | | var bookmark = await bookmarkStore.FindAsync(filter, cancellationToken); |
| | | 80 | | |
| | 0 | 81 | | if (bookmark == null) |
| | | 82 | | { |
| | 0 | 83 | | logger.LogDebug("Bookmark not found in store for filter {@Filter}", filter); |
| | 0 | 84 | | return ResumeBookmarkResult.NotFound(); |
| | | 85 | | } |
| | | 86 | | |
| | 0 | 87 | | var workflowClient = await workflowRuntime.CreateClientAsync(bookmark.WorkflowInstanceId, cancellationToken); |
| | 0 | 88 | | var runRequest = new RunWorkflowInstanceRequest |
| | 0 | 89 | | { |
| | 0 | 90 | | Input = options?.Input, |
| | 0 | 91 | | Properties = options?.Properties, |
| | 0 | 92 | | BookmarkId = bookmark.Id |
| | 0 | 93 | | }; |
| | | 94 | | |
| | | 95 | | try |
| | | 96 | | { |
| | 0 | 97 | | var response = await workflowClient.RunInstanceAsync(runRequest, cancellationToken); |
| | 0 | 98 | | logger.LogDebug("Resumed workflow instance {WorkflowInstanceId} with bookmark {BookmarkId}", bookmark.Workfl |
| | 0 | 99 | | return ResumeBookmarkResult.Found(response); |
| | | 100 | | } |
| | 0 | 101 | | catch (WorkflowInstanceNotFoundException) |
| | | 102 | | { |
| | | 103 | | // The workflow instance does not (yet) exist in the DB. |
| | 0 | 104 | | logger.LogDebug("No workflow instance with ID {WorkflowInstanceId} found for bookmark {BookmarkId} at this t |
| | 0 | 105 | | return ResumeBookmarkResult.NotFound(); |
| | | 106 | | } |
| | 0 | 107 | | } |
| | | 108 | | } |