| | | 1 | | using Elsa.Workflows; |
| | | 2 | | using Elsa.Workflows.Models; |
| | | 3 | | using Elsa.Workflows.Options; |
| | | 4 | | using Elsa.Workflows.State; |
| | | 5 | | using JetBrains.Annotations; |
| | | 6 | | |
| | | 7 | | // ReSharper disable once CheckNamespace |
| | | 8 | | namespace Elsa.Extensions; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Contains extension methods for <see cref="IWorkflowRunner"/>. |
| | | 12 | | /// </summary> |
| | | 13 | | [PublicAPI] |
| | | 14 | | public static class WorkflowRunnerExtensions |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Runs a workflow until its end, automatically resuming any bookmark it encounters. |
| | | 18 | | /// </summary> |
| | | 19 | | public static async Task<WorkflowState> RunUntilEndAsync( |
| | | 20 | | this IWorkflowRunner workflowRunner, |
| | | 21 | | IActivity activity, |
| | | 22 | | Func<WorkflowState, Bookmark, RunWorkflowOptions, bool>? processBookmark = default, |
| | | 23 | | CancellationToken cancellationToken = default) |
| | | 24 | | { |
| | 0 | 25 | | var result = await workflowRunner.RunAsync(activity, cancellationToken: cancellationToken); |
| | 0 | 26 | | var workflow = result.Workflow; |
| | 0 | 27 | | var workflowState = result.WorkflowState; |
| | 0 | 28 | | var bookmarks = new Stack<Bookmark>(workflowState.Bookmarks); |
| | | 29 | | |
| | | 30 | | // Continue resuming the workflow for as long as there are bookmarks to resume. |
| | 0 | 31 | | while (bookmarks.TryPop(out var bookmark)) |
| | | 32 | | { |
| | 0 | 33 | | var options = new RunWorkflowOptions { BookmarkId = bookmark.Id }; |
| | | 34 | | |
| | | 35 | | // Give caller a chance to determine whether or not to resume the bookmark, as well as configure any input t |
| | 0 | 36 | | if (processBookmark != null && !processBookmark(workflowState, bookmark, options)) |
| | | 37 | | continue; |
| | | 38 | | |
| | 0 | 39 | | result = await workflowRunner.RunAsync(workflow, workflowState, options, cancellationToken: cancellationToke |
| | 0 | 40 | | workflowState = result.WorkflowState; |
| | | 41 | | |
| | 0 | 42 | | foreach (var newBookmark in workflowState.Bookmarks) |
| | 0 | 43 | | bookmarks.Push(newBookmark); |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | // Return the workflow state. |
| | 0 | 47 | | return workflowState; |
| | 0 | 48 | | } |
| | | 49 | | } |