| | | 1 | | using System.ComponentModel; |
| | | 2 | | using System.Runtime.CompilerServices; |
| | | 3 | | using Elsa.Workflows.Attributes; |
| | | 4 | | using Elsa.Workflows.Options; |
| | | 5 | | |
| | | 6 | | namespace Elsa.Workflows.Activities; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Execute a set of activities in parallel. |
| | | 10 | | /// </summary> |
| | | 11 | | [Activity("Elsa", "Workflows", "Execute a set of activities in parallel.")] |
| | | 12 | | [Browsable(false)] // Hidden from the designer until we have Sequential activity designer support. |
| | | 13 | | public class Parallel : Container |
| | | 14 | | { |
| | | 15 | | private const string ScheduledChildrenProperty = "ScheduledChildren"; |
| | | 16 | | |
| | | 17 | | /// <inheritdoc /> |
| | 15 | 18 | | public Parallel([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, line) |
| | | 19 | | { |
| | 15 | 20 | | } |
| | | 21 | | |
| | | 22 | | /// <inheritdoc /> |
| | 13 | 23 | | public Parallel(params IActivity[] activities) : this() |
| | | 24 | | { |
| | 13 | 25 | | Activities = activities; |
| | 13 | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <inheritdoc /> |
| | | 29 | | protected override async ValueTask ScheduleChildrenAsync(ActivityExecutionContext context) |
| | | 30 | | { |
| | | 31 | | // If there are no activities, complete immediately |
| | 15 | 32 | | if (Activities.Count == 0) |
| | | 33 | | { |
| | 2 | 34 | | await context.CompleteActivityAsync(); |
| | 2 | 35 | | return; |
| | | 36 | | } |
| | | 37 | | |
| | 13 | 38 | | context.SetProperty(ScheduledChildrenProperty, Activities.Count); |
| | | 39 | | |
| | | 40 | | // For Parallel, all children are scheduled by the parent activity (this), so the scheduling activity is the Par |
| | 13 | 41 | | var options = new ScheduleWorkOptions |
| | 13 | 42 | | { |
| | 13 | 43 | | CompletionCallback = OnChildCompleted, |
| | 13 | 44 | | SchedulingActivityExecutionId = context.Id |
| | 13 | 45 | | }; |
| | | 46 | | |
| | 96 | 47 | | foreach (var activity in Activities) |
| | 35 | 48 | | await context.ScheduleActivityAsync(activity, options); |
| | 15 | 49 | | } |
| | | 50 | | |
| | | 51 | | private static async ValueTask OnChildCompleted(ActivityCompletedContext context) |
| | | 52 | | { |
| | 36 | 53 | | var remainingChildren = context.TargetContext.UpdateProperty<int>(ScheduledChildrenProperty, scheduledChildren = |
| | | 54 | | |
| | 18 | 55 | | if (remainingChildren == 0) |
| | 6 | 56 | | await context.TargetContext.CompleteActivityAsync(); |
| | 18 | 57 | | } |
| | | 58 | | } |