| | | 1 | | using Elsa.Extensions; |
| | | 2 | | using Elsa.Workflows.Models; |
| | | 3 | | using Elsa.Workflows.Options; |
| | | 4 | | |
| | | 5 | | namespace Elsa.Workflows; |
| | | 6 | | |
| | | 7 | | /// <inheritdoc /> |
| | | 8 | | public class ActivityExecutionContextSchedulerStrategy : IActivityExecutionContextSchedulerStrategy |
| | | 9 | | { |
| | | 10 | | /// <inheritdoc /> |
| | | 11 | | public async Task ScheduleActivityAsync(ActivityExecutionContext context, IActivity? activity, ActivityExecutionCont |
| | | 12 | | { |
| | 2593 | 13 | | var activityNode = activity != null |
| | 2593 | 14 | | ? context.WorkflowExecutionContext.FindNodeByActivity(activity) ?? throw new InvalidOperationException("The |
| | 2593 | 15 | | : null; |
| | 2593 | 16 | | await ScheduleActivityAsync(context, activityNode, owner, options); |
| | 2593 | 17 | | } |
| | | 18 | | |
| | | 19 | | /// <inheritdoc /> |
| | | 20 | | public async Task ScheduleActivityAsync(ActivityExecutionContext context, ActivityNode? activityNode, ActivityExecut |
| | | 21 | | { |
| | 2593 | 22 | | var workflowExecutionContext = context.WorkflowExecutionContext; |
| | 2593 | 23 | | if (context.GetIsBackgroundExecution()) |
| | | 24 | | { |
| | | 25 | | // Validate that the specified activity is part of the workflow. |
| | 0 | 26 | | if (activityNode != null && !workflowExecutionContext.NodeActivityLookup.ContainsKey(activityNode.Activity)) |
| | 0 | 27 | | throw new InvalidOperationException("The specified activity is not part of the workflow."); |
| | | 28 | | |
| | 0 | 29 | | var scheduledActivity = new ScheduledActivity |
| | 0 | 30 | | { |
| | 0 | 31 | | ActivityNodeId = activityNode?.NodeId, |
| | 0 | 32 | | OwnerActivityInstanceId = owner?.Id, |
| | 0 | 33 | | Options = options != null |
| | 0 | 34 | | ? new ScheduledActivityOptions |
| | 0 | 35 | | { |
| | 0 | 36 | | CompletionCallback = options?.CompletionCallback?.Method.Name, |
| | 0 | 37 | | Tag = options?.Tag, |
| | 0 | 38 | | ExistingActivityInstanceId = options?.ExistingActivityExecutionContext?.Id, |
| | 0 | 39 | | PreventDuplicateScheduling = options?.PreventDuplicateScheduling ?? false, |
| | 0 | 40 | | Variables = options?.Variables?.ToList(), |
| | 0 | 41 | | Input = options?.Input |
| | 0 | 42 | | } |
| | 0 | 43 | | : null |
| | 0 | 44 | | }; |
| | | 45 | | |
| | 0 | 46 | | var scheduledActivities = context.GetBackgroundScheduledActivities().ToList(); |
| | 0 | 47 | | scheduledActivities.Add(scheduledActivity); |
| | 0 | 48 | | context.SetBackgroundScheduledActivities(scheduledActivities); |
| | 0 | 49 | | return; |
| | | 50 | | } |
| | | 51 | | |
| | 2593 | 52 | | var completionCallback = options?.CompletionCallback; |
| | 2593 | 53 | | owner ??= context; |
| | | 54 | | |
| | 2593 | 55 | | if (activityNode == null) |
| | | 56 | | { |
| | 10 | 57 | | if (completionCallback != null) |
| | | 58 | | { |
| | 10 | 59 | | context.Tag = options?.Tag; |
| | 10 | 60 | | var completedContext = new ActivityCompletedContext(context, context); |
| | 10 | 61 | | await completionCallback(completedContext); |
| | | 62 | | } |
| | | 63 | | else |
| | 0 | 64 | | await owner.CompleteActivityAsync(); |
| | | 65 | | |
| | 10 | 66 | | return; |
| | | 67 | | } |
| | | 68 | | |
| | 2583 | 69 | | workflowExecutionContext.Schedule(activityNode, owner, options); |
| | 2593 | 70 | | } |
| | | 71 | | } |