| | | 1 | | using System.ComponentModel; |
| | | 2 | | using System.Runtime.CompilerServices; |
| | | 3 | | using Elsa.Workflows.Attributes; |
| | | 4 | | using Elsa.Workflows.Options; |
| | | 5 | | using Elsa.Workflows.Signals; |
| | | 6 | | using JetBrains.Annotations; |
| | | 7 | | |
| | | 8 | | namespace Elsa.Workflows.Activities; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Execute a set of activities in sequence. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <remarks> |
| | | 14 | | /// Rescheduled direct children retain completion callback ownership by this sequence. |
| | | 15 | | /// </remarks> |
| | | 16 | | [Category("Workflows")] |
| | | 17 | | [Activity("Elsa", "Workflows", "Execute a set of activities in sequence.")] |
| | | 18 | | [PublicAPI] |
| | | 19 | | [Browsable(false)] |
| | | 20 | | public class Sequence : Container |
| | | 21 | | { |
| | | 22 | | private const string CurrentIndexProperty = "CurrentIndex"; |
| | | 23 | | |
| | | 24 | | /// <inheritdoc /> |
| | 10076 | 25 | | public Sequence([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, line) |
| | | 26 | | { |
| | 10076 | 27 | | OnSignalReceived<BreakSignal>(OnBreakSignalReceived); |
| | 10076 | 28 | | OnSignalReceived<ScheduleChildActivity>(OnScheduleChildActivityAsync); |
| | 10076 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <inheritdoc /> |
| | | 32 | | protected override async ValueTask ScheduleChildrenAsync(ActivityExecutionContext context) |
| | | 33 | | { |
| | 163 | 34 | | await HandleItemAsync(context); |
| | 163 | 35 | | } |
| | | 36 | | |
| | | 37 | | private async ValueTask HandleItemAsync(ActivityExecutionContext context, ActivityExecutionContext? completedChildCo |
| | | 38 | | { |
| | 440 | 39 | | var currentIndex = context.GetProperty<int>(CurrentIndexProperty); |
| | 440 | 40 | | var childActivities = Activities.ToList(); |
| | | 41 | | |
| | 440 | 42 | | if (currentIndex >= childActivities.Count) |
| | | 43 | | { |
| | 117 | 44 | | await context.CompleteActivityAsync(); |
| | 117 | 45 | | return; |
| | | 46 | | } |
| | | 47 | | |
| | 323 | 48 | | var nextActivity = childActivities.ElementAt(currentIndex); |
| | 323 | 49 | | var options = new ScheduleWorkOptions |
| | 323 | 50 | | { |
| | 323 | 51 | | CompletionCallback = OnChildCompleted, |
| | 323 | 52 | | SchedulingActivityExecutionId = completedChildContext?.Id |
| | 323 | 53 | | }; |
| | 323 | 54 | | await context.ScheduleActivityAsync(nextActivity, options); |
| | 646 | 55 | | context.UpdateProperty<int>(CurrentIndexProperty, x => x + 1); |
| | 440 | 56 | | } |
| | | 57 | | |
| | | 58 | | private async ValueTask OnChildCompleted(ActivityCompletedContext context) |
| | | 59 | | { |
| | 288 | 60 | | var targetContext = context.TargetContext; |
| | 288 | 61 | | var childContext = context.ChildContext; |
| | 288 | 62 | | var isBreaking = targetContext.GetIsBreaking(); |
| | 288 | 63 | | var completedActivity = childContext.Activity; |
| | | 64 | | |
| | | 65 | | // If the complete activity is a terminal node, complete the sequence immediately. |
| | 288 | 66 | | if (isBreaking || completedActivity is ITerminalNode) |
| | | 67 | | { |
| | 11 | 68 | | await targetContext.CompleteActivityAsync(); |
| | 11 | 69 | | return; |
| | | 70 | | } |
| | | 71 | | |
| | 277 | 72 | | await HandleItemAsync(targetContext, childContext); |
| | 288 | 73 | | } |
| | | 74 | | |
| | | 75 | | private async ValueTask OnScheduleChildActivityAsync(ScheduleChildActivity signal, SignalContext context) |
| | | 76 | | { |
| | 0 | 77 | | var sequenceContext = context.ReceiverActivityExecutionContext; |
| | 0 | 78 | | var childActivity = signal.ActivityExecutionContext?.Activity ?? signal.Activity; |
| | | 79 | | |
| | 0 | 80 | | if (childActivity == null || !Activities.Contains(childActivity)) |
| | | 81 | | { |
| | 0 | 82 | | return; |
| | | 83 | | } |
| | | 84 | | |
| | 0 | 85 | | context.StopPropagation(); |
| | 0 | 86 | | await sequenceContext.ScheduleActivityAsync(childActivity, new ScheduleWorkOptions |
| | 0 | 87 | | { |
| | 0 | 88 | | ExistingActivityExecutionContext = signal.ActivityExecutionContext, |
| | 0 | 89 | | CompletionCallback = OnChildCompleted, |
| | 0 | 90 | | Input = signal.Input |
| | 0 | 91 | | }); |
| | 0 | 92 | | } |
| | | 93 | | |
| | | 94 | | private void OnBreakSignalReceived(BreakSignal signal, SignalContext signalContext) |
| | | 95 | | { |
| | 0 | 96 | | signalContext.ReceiverActivityExecutionContext.SetIsBreaking(); |
| | 0 | 97 | | } |
| | | 98 | | } |