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