| | | 1 | | using Elsa.Workflows; |
| | | 2 | | using Elsa.Workflows.Memory; |
| | | 3 | | using Elsa.Workflows.Models; |
| | | 4 | | using Elsa.Workflows.Options; |
| | | 5 | | using Microsoft.Extensions.Logging; |
| | | 6 | | |
| | | 7 | | // ReSharper disable once CheckNamespace |
| | | 8 | | namespace Elsa.Extensions; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Adds extension methods to <see cref="ActivityExecutionContext"/>. |
| | | 12 | | /// </summary> |
| | | 13 | | public static class WorkflowExecutionContextExtensions |
| | | 14 | | { |
| | | 15 | | extension(WorkflowExecutionContext workflowExecutionContext) |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Schedules the workflow for execution. |
| | | 19 | | /// </summary> |
| | | 20 | | public ActivityWorkItem ScheduleWorkflow(IDictionary<string, object>? input = null, IEnumerable<Variable>? varia |
| | | 21 | | { |
| | 400 | 22 | | var workflow = workflowExecutionContext.Workflow; |
| | 400 | 23 | | var workItem = new ActivityWorkItem(workflow, input: input, variables: variables); |
| | 400 | 24 | | workflowExecutionContext.Scheduler.Schedule(workItem); |
| | 400 | 25 | | return workItem; |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Schedules the root activity of the workflow. |
| | | 30 | | /// </summary> |
| | | 31 | | public ActivityWorkItem ScheduleRoot(IDictionary<string, object>? input = null, IEnumerable<Variable>? variables |
| | | 32 | | { |
| | 0 | 33 | | var workflow = workflowExecutionContext.Workflow; |
| | 0 | 34 | | var workItem = new ActivityWorkItem(workflow.Root, input: input, variables: variables); |
| | 0 | 35 | | workflowExecutionContext.Scheduler.Schedule(workItem); |
| | 0 | 36 | | return workItem; |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Schedules the specified activity of the workflow. |
| | | 41 | | /// </summary> |
| | | 42 | | public ActivityWorkItem ScheduleActivity(IActivity activity, IDictionary<string, object>? input = null, IEnumera |
| | | 43 | | { |
| | 0 | 44 | | var workItem = new ActivityWorkItem(activity, input: input, variables: variables); |
| | 0 | 45 | | workflowExecutionContext.Scheduler.Schedule(workItem); |
| | 0 | 46 | | return workItem; |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Schedules the specified activity execution context of the workflow. |
| | | 51 | | /// </summary> |
| | | 52 | | public ActivityWorkItem ScheduleActivityExecutionContext(ActivityExecutionContext activityExecutionContext, IDic |
| | | 53 | | { |
| | 0 | 54 | | var workItem = new ActivityWorkItem( |
| | 0 | 55 | | activityExecutionContext.Activity, |
| | 0 | 56 | | input: input, |
| | 0 | 57 | | variables: variables, |
| | 0 | 58 | | existingActivityExecutionContext: activityExecutionContext); |
| | 0 | 59 | | workflowExecutionContext.Scheduler.Schedule(workItem); |
| | 0 | 60 | | return workItem; |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Schedules the activity of the specified bookmark. |
| | | 65 | | /// </summary> |
| | | 66 | | /// <returns>The created work item, or <c>null</c> if the specified bookmark doesn't exist in the <see cref="Wor |
| | | 67 | | public ActivityWorkItem? ScheduleBookmark(Bookmark bookmark, IDictionary<string, object>? input = null, IEnumera |
| | | 68 | | { |
| | | 69 | | // Get the activity execution context that owns the bookmark. |
| | 78 | 70 | | var bookmarkedActivityContext = workflowExecutionContext.ActivityExecutionContexts.FirstOrDefault(x => x.Id |
| | 36 | 71 | | var logger = workflowExecutionContext.GetRequiredService<ILogger<WorkflowExecutionContext>>(); |
| | | 72 | | |
| | 36 | 73 | | if (bookmarkedActivityContext == null) |
| | | 74 | | { |
| | 0 | 75 | | logger.LogWarning("Could not find activity execution context with ID {ActivityInstanceId} for bookmark { |
| | 0 | 76 | | return null; |
| | | 77 | | } |
| | | 78 | | |
| | 36 | 79 | | var bookmarkedActivity = bookmarkedActivityContext.Activity; |
| | | 80 | | |
| | | 81 | | // Schedule the activity to resume. |
| | 36 | 82 | | var workItem = new ActivityWorkItem(bookmarkedActivity) |
| | 36 | 83 | | { |
| | 36 | 84 | | ExistingActivityExecutionContext = bookmarkedActivityContext, |
| | 36 | 85 | | Input = input ?? new Dictionary<string, object>(), |
| | 36 | 86 | | Variables = variables |
| | 36 | 87 | | }; |
| | 36 | 88 | | workflowExecutionContext.Scheduler.Schedule(workItem); |
| | | 89 | | |
| | | 90 | | // If no resumption point was specified, use a "noop" to prevent the regular "ExecuteAsync" method to be inv |
| | | 91 | | // Unless the bookmark is configured to auto-complete, in which case we'll just complete the activity. |
| | 36 | 92 | | workflowExecutionContext.ExecuteDelegate = bookmark.CallbackMethodName != null |
| | 36 | 93 | | ? bookmarkedActivity.GetResumeActivityDelegate(bookmark.CallbackMethodName) |
| | 36 | 94 | | : bookmark.AutoComplete |
| | 36 | 95 | | ? WorkflowExecutionContext.Complete |
| | 36 | 96 | | : WorkflowExecutionContext.Noop; |
| | | 97 | | |
| | | 98 | | // Store the bookmark to resume in the context. |
| | 36 | 99 | | workflowExecutionContext.ResumedBookmarkContext = new(bookmark); |
| | 36 | 100 | | logger.LogDebug("Scheduled activity {ActivityId} to resume from bookmark {BookmarkId}", bookmarkedActivity.I |
| | | 101 | | |
| | 36 | 102 | | return workItem; |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Schedules the specified activity. |
| | | 107 | | /// </summary> |
| | | 108 | | public ActivityWorkItem Schedule(ActivityNode activityNode, |
| | | 109 | | ActivityExecutionContext owner, |
| | | 110 | | ScheduleWorkOptions? options = null) |
| | | 111 | | { |
| | 2707 | 112 | | var schedulerStrategy = workflowExecutionContext.GetRequiredService<IWorkflowExecutionContextSchedulerStrate |
| | 2707 | 113 | | return schedulerStrategy.Schedule(workflowExecutionContext, activityNode, owner, options); |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | /// <summary> |
| | | 117 | | /// Returns true if all activities have completed or canceled, false otherwise. |
| | | 118 | | /// </summary> |
| | 3333 | 119 | | public bool AllActivitiesCompleted() => workflowExecutionContext.ActivityExecutionContexts.All(x => x.IsComplete |
| | | 120 | | |
| | | 121 | | public object? GetOutputByActivityId(string activityId, string? outputName = null) |
| | | 122 | | { |
| | 11 | 123 | | var outputRegister = workflowExecutionContext.GetActivityOutputRegister(); |
| | 11 | 124 | | return outputRegister.FindOutputByActivityId(activityId, outputName); |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | public IEnumerable<ActivityExecutionContext> FindActivityExecutionContexts(ActivityHandle activityHandle) |
| | | 128 | | { |
| | 0 | 129 | | if (activityHandle.ActivityInstanceId != null) |
| | 0 | 130 | | return workflowExecutionContext.ActivityExecutionContexts.Where(x => x.Id == activityHandle.ActivityInst |
| | 0 | 131 | | if (activityHandle.ActivityNodeId != null) |
| | 0 | 132 | | return workflowExecutionContext.ActivityExecutionContexts.Where(x => x.NodeId == activityHandle.Activity |
| | 0 | 133 | | if (activityHandle.ActivityId != null) |
| | 0 | 134 | | return workflowExecutionContext.ActivityExecutionContexts.Where(x => x.Activity.Id == activityHandle.Act |
| | 0 | 135 | | if (activityHandle.ActivityHash != null) |
| | | 136 | | { |
| | 0 | 137 | | var activity = workflowExecutionContext.FindActivityByHash(activityHandle.ActivityHash); |
| | 0 | 138 | | return activity != null ? workflowExecutionContext.ActivityExecutionContexts.Where(x => x.Activity.NodeI |
| | | 139 | | } |
| | | 140 | | |
| | 0 | 141 | | return []; |
| | | 142 | | } |
| | | 143 | | } |
| | | 144 | | } |