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