| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using System.Text.Json.Nodes; |
| | | 4 | | using Elsa.Expressions.Helpers; |
| | | 5 | | using Elsa.Expressions.Models; |
| | | 6 | | using Elsa.Extensions; |
| | | 7 | | using Elsa.Workflows.Attributes; |
| | | 8 | | using Elsa.Workflows.Memory; |
| | | 9 | | using Elsa.Workflows.Models; |
| | | 10 | | |
| | | 11 | | namespace Elsa.Workflows.Activities; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Schedule an activity for each item in parallel. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <typeparam name="T"></typeparam> |
| | | 17 | | [Activity("Elsa", "Looping", "Schedule an activity for each item in parallel.")] |
| | | 18 | | public class ParallelForEach<T> : Activity |
| | | 19 | | { |
| | | 20 | | private const string ScheduledTagsProperty = nameof(ScheduledTagsProperty); |
| | | 21 | | private const string CompletedTagsProperty = nameof(CompletedTagsProperty); |
| | | 22 | | |
| | | 23 | | /// <inheritdoc /> |
| | 0 | 24 | | public ParallelForEach(Func<ExpressionExecutionContext, ICollection<T>> @delegate, [CallerFilePath] string? source = |
| | | 25 | | { |
| | 0 | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <inheritdoc /> |
| | 0 | 29 | | public ParallelForEach(Func<ICollection<T>> @delegate, [CallerFilePath] string? source = null, [CallerLineNumber] in |
| | | 30 | | { |
| | 0 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <inheritdoc /> |
| | 14 | 34 | | public ParallelForEach(ICollection<T> items, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = |
| | | 35 | | { |
| | 14 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <inheritdoc /> |
| | 14 | 39 | | public ParallelForEach(Input<object> items, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = n |
| | | 40 | | { |
| | 14 | 41 | | Items = items; |
| | 14 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <inheritdoc /> |
| | 16 | 45 | | public ParallelForEach([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, l |
| | | 46 | | { |
| | 16 | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// The items to iterate. |
| | | 51 | | /// </summary> |
| | | 52 | | [Input(Description = "The items to iterate through.")] |
| | 65 | 53 | | public Input<object> Items { get; set; } = new(Array.Empty<T>()); |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// The <see cref="IActivity"/> to execute each iteration. |
| | | 57 | | /// </summary> |
| | | 58 | | [Port] |
| | 56 | 59 | | public IActivity Body { get; set; } = null!; |
| | | 60 | | |
| | | 61 | | /// <inheritdoc /> |
| | | 62 | | protected override async ValueTask ExecuteAsync(ActivityExecutionContext context) |
| | | 63 | | { |
| | 14 | 64 | | var items = context.GetItemSource<T>(Items); |
| | 14 | 65 | | var tags = new List<Guid>(); |
| | 14 | 66 | | var currentIndex = 0; |
| | | 67 | | |
| | 84 | 68 | | await foreach (var item in items) |
| | | 69 | | { |
| | | 70 | | // For each item, declare a new variable for the work to be scheduled. |
| | 28 | 71 | | var currentValueVariable = new Variable<T>("CurrentValue", item) |
| | 28 | 72 | | { |
| | 28 | 73 | | // TODO: This should be configurable, because this won't work for e.g. file streams and other non-serial |
| | 28 | 74 | | StorageDriverType = typeof(WorkflowInstanceStorageDriver) |
| | 28 | 75 | | }; |
| | | 76 | | |
| | 28 | 77 | | var currentIndexVariable = new Variable<int>("CurrentIndex", currentIndex++) { StorageDriverType = typeof(Wo |
| | 28 | 78 | | var variables = new List<Variable> { currentValueVariable, currentIndexVariable }; |
| | | 79 | | |
| | | 80 | | // Schedule a body of work for each item. |
| | 28 | 81 | | var tag = Guid.NewGuid(); |
| | 28 | 82 | | tags.Add(tag); |
| | 28 | 83 | | await context.ScheduleActivityAsync(Body, OnChildCompleted, tag, variables); |
| | | 84 | | } |
| | | 85 | | |
| | 14 | 86 | | SetTagList(context, ScheduledTagsProperty, tags); |
| | 14 | 87 | | SetTagList(context, CompletedTagsProperty, new List<Guid>()); |
| | | 88 | | |
| | | 89 | | // If there were no items, we're done. |
| | 14 | 90 | | if (tags.Count == 0) |
| | 3 | 91 | | await context.CompleteActivityAsync(); |
| | 14 | 92 | | } |
| | | 93 | | |
| | | 94 | | private async ValueTask OnChildCompleted(ActivityCompletedContext context) |
| | | 95 | | { |
| | 16 | 96 | | var targetContext = context.TargetContext; |
| | 16 | 97 | | var scheduledTags = GetTagList(targetContext, ScheduledTagsProperty); |
| | 16 | 98 | | var completedTag = targetContext.Tag.ConvertTo<Guid>(); |
| | 16 | 99 | | var completedTags = GetTagList(targetContext, CompletedTagsProperty); |
| | | 100 | | |
| | 16 | 101 | | completedTags.Add(completedTag); |
| | 16 | 102 | | SetTagList(targetContext, CompletedTagsProperty, completedTags); |
| | | 103 | | |
| | | 104 | | // If not all scheduled activities have completed yet, we're not done yet. |
| | 16 | 105 | | if (!scheduledTags.IsEqualTo(completedTags)) |
| | 11 | 106 | | return; |
| | | 107 | | |
| | | 108 | | // We're done, so complete the activity. |
| | 5 | 109 | | await targetContext.CompleteActivityAsync(); |
| | 16 | 110 | | } |
| | | 111 | | |
| | | 112 | | private ICollection<Guid> GetTagList(ActivityExecutionContext context, string propertyName) |
| | | 113 | | { |
| | | 114 | | // Read the list of tags from the context using the specified property name. The value is stored as JsonArray, s |
| | 32 | 115 | | var jsonArray = context.GetProperty<JsonArray>(propertyName)!; |
| | 98 | 116 | | return jsonArray.Select(x => x.ConvertTo<Guid>()).ToList(); |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | private void SetTagList(ActivityExecutionContext context, string propertyName, ICollection<Guid> tags) |
| | | 120 | | { |
| | | 121 | | // Serialize the list of tags to a JsonArray and store it in the context using the specified property name. |
| | 44 | 122 | | var jsonArray = JsonSerializer.SerializeToNode(tags) as JsonArray; |
| | 44 | 123 | | context.SetProperty(propertyName, jsonArray); |
| | 44 | 124 | | } |
| | | 125 | | } |