| | | 1 | | using Elsa.Workflows.Models; |
| | | 2 | | using Elsa.Workflows.Options; |
| | | 3 | | |
| | | 4 | | namespace Elsa.Workflows; |
| | | 5 | | |
| | | 6 | | /// <inheritdoc /> |
| | | 7 | | public class WorkflowExecutionContextSchedulerStrategy : IWorkflowExecutionContextSchedulerStrategy |
| | | 8 | | { |
| | | 9 | | /// <inheritdoc /> |
| | | 10 | | public ActivityWorkItem Schedule(WorkflowExecutionContext context, ActivityNode activityNode, ActivityExecutionConte |
| | | 11 | | { |
| | | 12 | | // Validate that the specified activity is part of the workflow. |
| | 2816 | 13 | | if (!context.NodeActivityLookup.ContainsKey(activityNode.Activity)) |
| | 0 | 14 | | throw new InvalidOperationException("The specified activity is not part of the workflow."); |
| | | 15 | | |
| | 2816 | 16 | | var scheduler = context.Scheduler; |
| | | 17 | | |
| | 2816 | 18 | | if (options?.PreventDuplicateScheduling == true) |
| | | 19 | | { |
| | | 20 | | // Check if the activity is already scheduled for the specified owner. |
| | 0 | 21 | | var existingWorkItem = scheduler.Find(x => x.Activity.NodeId == activityNode.NodeId && x.Owner == owner); |
| | | 22 | | |
| | 0 | 23 | | if (existingWorkItem != null) |
| | 0 | 24 | | return existingWorkItem; |
| | | 25 | | } |
| | | 26 | | |
| | 2816 | 27 | | var activity = activityNode.Activity; |
| | 2816 | 28 | | var tag = options?.Tag; |
| | | 29 | | |
| | | 30 | | // Use explicit SchedulingActivityExecutionId from options, or fall back to owner context. |
| | 2816 | 31 | | var schedulingActivityExecutionId = options?.SchedulingActivityExecutionId ?? owner.Id; |
| | | 32 | | |
| | | 33 | | // Use explicit SchedulingWorkflowInstanceId from options, if any. |
| | 2816 | 34 | | var schedulingWorkflowInstanceId = options?.SchedulingWorkflowInstanceId; |
| | | 35 | | |
| | 2816 | 36 | | var workItem = new ActivityWorkItem( |
| | 2816 | 37 | | activity, |
| | 2816 | 38 | | owner, |
| | 2816 | 39 | | tag, |
| | 2816 | 40 | | options?.Variables, |
| | 2816 | 41 | | options?.ExistingActivityExecutionContext, |
| | 2816 | 42 | | options?.Input, |
| | 2816 | 43 | | schedulingActivityExecutionId, |
| | 2816 | 44 | | schedulingWorkflowInstanceId); |
| | 2816 | 45 | | var completionCallback = options?.CompletionCallback; |
| | | 46 | | |
| | 2816 | 47 | | context.AddCompletionCallback(owner, activityNode, completionCallback, tag); |
| | 2816 | 48 | | scheduler.Schedule(workItem); |
| | | 49 | | |
| | 2816 | 50 | | return workItem; |
| | | 51 | | } |
| | | 52 | | } |