| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | using Elsa.Common.Models; |
| | | 3 | | using Elsa.Expressions.Contracts; |
| | | 4 | | using Elsa.Expressions.Helpers; |
| | | 5 | | using Elsa.Expressions.Models; |
| | | 6 | | using Elsa.Extensions; |
| | | 7 | | using Elsa.Workflows.Activities.Flowchart.Attributes; |
| | | 8 | | using Elsa.Workflows.Attributes; |
| | | 9 | | using Elsa.Workflows.Helpers; |
| | | 10 | | using Elsa.Workflows.Management; |
| | | 11 | | using Elsa.Workflows.Memory; |
| | | 12 | | using Elsa.Workflows.Models; |
| | | 13 | | using Elsa.Workflows.Options; |
| | | 14 | | using Elsa.Workflows.Runtime.Notifications; |
| | | 15 | | using Elsa.Workflows.Runtime.Requests; |
| | | 16 | | using Elsa.Workflows.Runtime.Stimuli; |
| | | 17 | | using Elsa.Workflows.Runtime.UIHints; |
| | | 18 | | using Elsa.Workflows.UIHints; |
| | | 19 | | using JetBrains.Annotations; |
| | | 20 | | |
| | | 21 | | namespace Elsa.Workflows.Runtime.Activities; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Creates new workflow instances of the specified workflow for each item in the data source and dispatches them for ex |
| | | 25 | | /// </summary> |
| | | 26 | | [Activity("Elsa", "Composition", "Create new workflow instances for each item in the data source and dispatch them for e |
| | | 27 | | [FlowNode("Completed", "Canceled", "Done")] |
| | | 28 | | [UsedImplicitly] |
| | | 29 | | public class BulkDispatchWorkflows : Activity |
| | | 30 | | { |
| | | 31 | | private const string DispatchedInstancesCountKey = nameof(DispatchedInstancesCountKey); |
| | | 32 | | private const string CompletedInstancesCountKey = nameof(CompletedInstancesCountKey); |
| | | 33 | | |
| | | 34 | | /// <inheritdoc /> |
| | 503 | 35 | | public BulkDispatchWorkflows([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(sou |
| | | 36 | | { |
| | 503 | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// The definition ID of the workflows to dispatch. |
| | | 41 | | /// </summary> |
| | | 42 | | [Input( |
| | | 43 | | DisplayName = "Workflow Definition", |
| | | 44 | | Description = "The definition ID of the workflows to dispatch.", |
| | | 45 | | UIHint = InputUIHints.WorkflowDefinitionPicker |
| | | 46 | | )] |
| | 1871 | 47 | | public Input<string> WorkflowDefinitionId { get; set; } = null!; |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// The data source to use for dispatching the workflows. |
| | | 51 | | /// </summary> |
| | | 52 | | [Input(Description = "The data source to use for dispatching the workflows.")] |
| | 1860 | 53 | | public Input<object> Items { get; set; } = null!; |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// The default key to use for the item input. Will not be used if the Items contain a list of dictionaries. |
| | | 57 | | /// </summary> |
| | | 58 | | [Input(Description = "The default key to use for the input name when sending the current item to the dispatched work |
| | 1870 | 59 | | public Input<string> DefaultItemInputKey { get; set; } = new("Item"); |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// The correlation ID to associate the workflow with. |
| | | 63 | | /// </summary> |
| | | 64 | | [Input( |
| | | 65 | | DisplayName = "Correlation ID Function", |
| | | 66 | | Description = "A function to compute the correlation ID to associate a dispatched workflow with.", |
| | | 67 | | AutoEvaluate = false)] |
| | 1451 | 68 | | public Input<string?>? CorrelationIdFunction { get; set; } |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// The input to send to the workflows. |
| | | 72 | | /// </summary> |
| | | 73 | | [Input(Description = "Additional input to send to the workflows being dispatched.")] |
| | 1373 | 74 | | public Input<IDictionary<string, object>?> Input { get; set; } = null!; |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// True to wait for the child workflow to complete before completing this activity, false to "fire and forget". |
| | | 78 | | /// </summary> |
| | | 79 | | [Input( |
| | | 80 | | Description = "Wait for the dispatched workflows to complete before completing this activity.", |
| | | 81 | | DefaultValue = true)] |
| | 2360 | 82 | | public Input<bool> WaitForCompletion { get; set; } = new(true); |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Indicates whether a new trace context should be started for the workflow execution. |
| | | 86 | | /// </summary> |
| | | 87 | | [Input(Description = "Start a new trace context when using Open Telemetry.", Category = "Open Telemetry")] |
| | 1860 | 88 | | public Input<bool> StartNewTrace { get; set; } = new(false); |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// The channel to dispatch the workflow to. |
| | | 92 | | /// </summary> |
| | | 93 | | [Input( |
| | | 94 | | DisplayName = "Channel", |
| | | 95 | | Description = "The channel to dispatch the workflow to.", |
| | | 96 | | UIHint = InputUIHints.DropDown, |
| | | 97 | | UIHandler = typeof(DispatcherChannelOptionsProvider) |
| | | 98 | | )] |
| | 1367 | 99 | | public Input<string?> ChannelName { get; set; } = null!; |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// An activity to execute when the child workflow finishes. |
| | | 103 | | /// </summary> |
| | 1551 | 104 | | [Port] public IActivity? ChildCompleted { get; set; } |
| | | 105 | | |
| | | 106 | | /// <summary> |
| | | 107 | | /// An activity to execute when the child workflow faults. |
| | | 108 | | /// </summary> |
| | 1596 | 109 | | [Port] public IActivity? ChildFaulted { get; set; } |
| | | 110 | | |
| | | 111 | | /// <inheritdoc /> |
| | | 112 | | protected override async ValueTask ExecuteAsync(ActivityExecutionContext context) |
| | | 113 | | { |
| | 9 | 114 | | var waitForCompletion = WaitForCompletion.GetOrDefault(context); |
| | 9 | 115 | | var startNewTrace = StartNewTrace.GetOrDefault(context); |
| | 9 | 116 | | var items = await context.GetItemSource<object>(Items).ToListAsync(context.CancellationToken); |
| | 9 | 117 | | var count = items.Count; |
| | | 118 | | |
| | | 119 | | // Dispatch the child workflows. |
| | 57 | 120 | | foreach (var item in items) |
| | 20 | 121 | | await DispatchChildWorkflowAsync(context, item, waitForCompletion, startNewTrace, count); |
| | | 122 | | |
| | | 123 | | // Store the number of dispatched instances for tracking. |
| | 8 | 124 | | context.SetProperty(DispatchedInstancesCountKey, count); |
| | | 125 | | |
| | | 126 | | // If we need to wait for the child workflows to complete (if any), create a bookmark. |
| | 8 | 127 | | if (waitForCompletion && count > 0) |
| | | 128 | | { |
| | 5 | 129 | | var workflowInstanceId = context.WorkflowExecutionContext.Id; |
| | 5 | 130 | | var bookmarkOptions = new CreateBookmarkArgs |
| | 5 | 131 | | { |
| | 5 | 132 | | Callback = OnChildWorkflowCompletedAsync, |
| | 5 | 133 | | Stimulus = new BulkDispatchWorkflowsStimulus(workflowInstanceId) |
| | 5 | 134 | | { |
| | 5 | 135 | | ParentInstanceId = context.WorkflowExecutionContext.Id, |
| | 5 | 136 | | ScheduledInstanceIdsCount = count |
| | 5 | 137 | | }, |
| | 5 | 138 | | IncludeActivityInstanceId = false, |
| | 5 | 139 | | AutoBurn = false, |
| | 5 | 140 | | }; |
| | | 141 | | |
| | 5 | 142 | | context.CreateBookmark(bookmarkOptions); |
| | | 143 | | } |
| | | 144 | | else |
| | | 145 | | { |
| | | 146 | | // Otherwise, we can complete immediately. |
| | 3 | 147 | | await context.CompleteActivityWithOutcomesAsync("Done"); |
| | | 148 | | } |
| | 8 | 149 | | } |
| | | 150 | | |
| | | 151 | | private async ValueTask<string> DispatchChildWorkflowAsync(ActivityExecutionContext context, object item, bool waitF |
| | | 152 | | { |
| | 20 | 153 | | var workflowDefinitionId = WorkflowDefinitionId.Get(context); |
| | 20 | 154 | | var workflowDefinitionService = context.GetRequiredService<IWorkflowDefinitionService>(); |
| | 20 | 155 | | var workflowGraph = await workflowDefinitionService.FindWorkflowGraphAsync(workflowDefinitionId, VersionOptions. |
| | | 156 | | |
| | 20 | 157 | | if (workflowGraph == null) |
| | 1 | 158 | | throw new($"No published version of workflow definition with ID {workflowDefinitionId} found."); |
| | | 159 | | |
| | 19 | 160 | | var parentInstanceId = context.WorkflowExecutionContext.Id; |
| | 19 | 161 | | var baseInput = Input.GetOrDefault(context); |
| | 19 | 162 | | var input = baseInput != null ? new Dictionary<string, object>(baseInput) : new Dictionary<string, object>(); |
| | 19 | 163 | | var channelName = ChannelName.GetOrDefault(context); |
| | 19 | 164 | | var defaultInputItemKey = DefaultItemInputKey.GetOrDefault(context, () => "Item")!; |
| | 19 | 165 | | var properties = new Dictionary<string, object> |
| | 19 | 166 | | { |
| | 19 | 167 | | ["ParentInstanceId"] = parentInstanceId |
| | 19 | 168 | | }; |
| | | 169 | | |
| | 32 | 170 | | if (waitForCompletion) properties["WaitForCompletion"] = true; |
| | 19 | 171 | | if (startNewTrace) properties["StartNewTrace"] = true; |
| | | 172 | | |
| | 19 | 173 | | if (waitForCompletion) |
| | | 174 | | { |
| | 13 | 175 | | var activityTypeName = ActivityTypeNameHelper.GenerateTypeName<BulkDispatchWorkflows>(); |
| | 13 | 176 | | var stimulus = new BulkDispatchWorkflowsStimulus(parentInstanceId) |
| | 13 | 177 | | { |
| | 13 | 178 | | ScheduledInstanceIdsCount = scheduledInstancesCount |
| | 13 | 179 | | }; |
| | 13 | 180 | | var stimulusHash = context.GetRequiredService<IStimulusHasher>().Hash(activityTypeName, stimulus); |
| | 13 | 181 | | DispatchWorkflowActivationDeniedRoute.Add(properties, activityTypeName, stimulusHash); |
| | | 182 | | } |
| | | 183 | | |
| | 19 | 184 | | var itemDictionary = new Dictionary<string, object> |
| | 19 | 185 | | { |
| | 19 | 186 | | [defaultInputItemKey] = item |
| | 19 | 187 | | }; |
| | | 188 | | |
| | 19 | 189 | | var evaluatorOptions = new ExpressionEvaluatorOptions |
| | 19 | 190 | | { |
| | 19 | 191 | | Arguments = itemDictionary |
| | 19 | 192 | | }; |
| | | 193 | | |
| | 19 | 194 | | var inputDictionary = item as IDictionary<string, object> ?? itemDictionary; |
| | 19 | 195 | | input["ParentInstanceId"] = parentInstanceId; |
| | 19 | 196 | | input.Merge(inputDictionary); |
| | | 197 | | |
| | 19 | 198 | | var workflowDispatcher = context.GetRequiredService<IWorkflowDispatcher>(); |
| | 19 | 199 | | var identityGenerator = context.GetRequiredService<IIdentityGenerator>(); |
| | 19 | 200 | | var evaluator = context.GetRequiredService<IExpressionEvaluator>(); |
| | 19 | 201 | | var correlationId = CorrelationIdFunction != null ? await evaluator.EvaluateAsync<string>(CorrelationIdFunction! |
| | 19 | 202 | | var instanceId = identityGenerator.GenerateId(); |
| | 19 | 203 | | var request = new DispatchWorkflowDefinitionRequest(workflowGraph.Workflow.Identity.Id) |
| | 19 | 204 | | { |
| | 19 | 205 | | ParentWorkflowInstanceId = parentInstanceId, |
| | 19 | 206 | | Input = input, |
| | 19 | 207 | | Properties = properties, |
| | 19 | 208 | | CorrelationId = correlationId, |
| | 19 | 209 | | InstanceId = instanceId |
| | 19 | 210 | | }; |
| | 19 | 211 | | var options = new DispatchWorkflowOptions |
| | 19 | 212 | | { |
| | 19 | 213 | | Channel = channelName |
| | 19 | 214 | | }; |
| | | 215 | | |
| | 19 | 216 | | await workflowDispatcher.DispatchAsync(request, options, context.CancellationToken); |
| | 19 | 217 | | return instanceId; |
| | 19 | 218 | | } |
| | | 219 | | |
| | | 220 | | private async ValueTask OnChildWorkflowCompletedAsync(ActivityExecutionContext context) |
| | | 221 | | { |
| | 13 | 222 | | var input = context.WorkflowInput; |
| | 13 | 223 | | var workflowInstanceId = input["WorkflowInstanceId"].ConvertTo<string>()!; |
| | 13 | 224 | | var workflowSubStatus = input["WorkflowSubStatus"].ConvertTo<WorkflowSubStatus>(); |
| | 13 | 225 | | var cannotStart = input.TryGetValue("CannotStart", out var cannotStartValue) && cannotStartValue is true; |
| | 13 | 226 | | var finishedInstancesCount = context.GetProperty<long>(CompletedInstancesCountKey) + 1; |
| | | 227 | | |
| | 13 | 228 | | context.SetProperty(CompletedInstancesCountKey, finishedInstancesCount); |
| | | 229 | | |
| | 13 | 230 | | var variables = new List<Variable>(); |
| | 13 | 231 | | if (!cannotStart) |
| | | 232 | | { |
| | 12 | 233 | | variables.Add(new Variable<string>("ChildInstanceId", workflowInstanceId) |
| | 12 | 234 | | { |
| | 12 | 235 | | StorageDriverType = typeof(WorkflowInstanceStorageDriver) |
| | 12 | 236 | | }); |
| | | 237 | | } |
| | | 238 | | |
| | 13 | 239 | | var options = new ScheduleWorkOptions |
| | 13 | 240 | | { |
| | 13 | 241 | | Input = input, |
| | 13 | 242 | | Variables = variables, |
| | 13 | 243 | | CompletionCallback = OnChildFinishedCompletedAsync |
| | 13 | 244 | | }; |
| | | 245 | | |
| | 13 | 246 | | switch (workflowSubStatus) |
| | | 247 | | { |
| | 4 | 248 | | case WorkflowSubStatus.Faulted when ChildFaulted is not null: |
| | 4 | 249 | | await context.ScheduleActivityAsync(ChildFaulted, options); |
| | 4 | 250 | | return; |
| | 9 | 251 | | case WorkflowSubStatus.Finished when ChildCompleted is not null: |
| | 0 | 252 | | await context.ScheduleActivityAsync(ChildCompleted, options); |
| | 0 | 253 | | return; |
| | | 254 | | default: |
| | 9 | 255 | | await AttemptToCompleteAsync(context); |
| | | 256 | | break; |
| | | 257 | | } |
| | 13 | 258 | | } |
| | | 259 | | |
| | | 260 | | private async ValueTask OnChildFinishedCompletedAsync(ActivityCompletedContext context) |
| | | 261 | | { |
| | 4 | 262 | | await AttemptToCompleteAsync(context.TargetContext); |
| | 4 | 263 | | } |
| | | 264 | | |
| | | 265 | | private async ValueTask AttemptToCompleteAsync(ActivityExecutionContext context) |
| | | 266 | | { |
| | 13 | 267 | | var dispatchedInstancesCount = context.GetProperty<long>(DispatchedInstancesCountKey); |
| | 13 | 268 | | var finishedInstancesCount = context.GetProperty<long>(CompletedInstancesCountKey); |
| | | 269 | | |
| | 13 | 270 | | if (finishedInstancesCount >= dispatchedInstancesCount) |
| | 5 | 271 | | await context.CompleteActivityWithOutcomesAsync("Completed", "Done"); |
| | 13 | 272 | | } |
| | | 273 | | } |