| | | 1 | | using Elsa.Extensions; |
| | | 2 | | using Elsa.Mediator.Contracts; |
| | | 3 | | using Elsa.Workflows.Activities; |
| | | 4 | | using Elsa.Workflows.CommitStates; |
| | | 5 | | using Elsa.Workflows.Memory; |
| | | 6 | | using Elsa.Workflows.Models; |
| | | 7 | | using Elsa.Workflows.Notifications; |
| | | 8 | | using Elsa.Workflows.Options; |
| | | 9 | | using Elsa.Workflows.State; |
| | | 10 | | using Microsoft.Extensions.Logging; |
| | | 11 | | |
| | | 12 | | namespace Elsa.Workflows; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | 420 | 15 | | public class WorkflowRunner( |
| | 420 | 16 | | IServiceProvider serviceProvider, |
| | 420 | 17 | | IWorkflowExecutionPipeline pipeline, |
| | 420 | 18 | | IWorkflowStateExtractor workflowStateExtractor, |
| | 420 | 19 | | IWorkflowBuilderFactory workflowBuilderFactory, |
| | 420 | 20 | | IWorkflowGraphBuilder workflowGraphBuilder, |
| | 420 | 21 | | IIdentityGenerator identityGenerator, |
| | 420 | 22 | | INotificationSender notificationSender, |
| | 420 | 23 | | ILoggerStateGenerator<WorkflowExecutionContext> loggerStateGenerator, |
| | 420 | 24 | | ICommitStateHandler commitStateHandler, |
| | 420 | 25 | | ILogger<WorkflowRunner> logger) |
| | | 26 | | : IWorkflowRunner |
| | | 27 | | { |
| | | 28 | | /// <inheritdoc /> |
| | | 29 | | public async Task<RunWorkflowResult> RunAsync(IActivity activity, RunWorkflowOptions? options = null, CancellationTo |
| | | 30 | | { |
| | 99 | 31 | | var workflow = Workflow.FromActivity(activity); |
| | 99 | 32 | | var workflowGraph = await workflowGraphBuilder.BuildAsync(workflow, cancellationToken); |
| | 99 | 33 | | return await RunAsync(workflowGraph, options, cancellationToken); |
| | 99 | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <inheritdoc /> |
| | | 37 | | public async Task<RunWorkflowResult> RunAsync(IWorkflow workflow, RunWorkflowOptions? options = null, CancellationTo |
| | | 38 | | { |
| | 0 | 39 | | var builder = workflowBuilderFactory.CreateBuilder(); |
| | 0 | 40 | | var workflowDefinition = await builder.BuildWorkflowAsync(workflow, cancellationToken); |
| | 0 | 41 | | return await RunAsync(workflowDefinition, options, cancellationToken); |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <inheritdoc /> |
| | | 45 | | public async Task<RunWorkflowResult<TResult>> RunAsync<TResult>(WorkflowBase<TResult> workflow, RunWorkflowOptions? |
| | | 46 | | { |
| | 0 | 47 | | var result = await RunAsync((IWorkflow)workflow, options, cancellationToken); |
| | 0 | 48 | | return new(result.WorkflowExecutionContext, result.WorkflowState, result.Workflow, (TResult)result.Result!, resu |
| | 0 | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <inheritdoc /> |
| | | 52 | | public async Task<RunWorkflowResult> RunAsync<T>(RunWorkflowOptions? options = null, CancellationToken cancellationT |
| | | 53 | | { |
| | 2 | 54 | | var builder = workflowBuilderFactory.CreateBuilder(); |
| | 2 | 55 | | var workflowDefinition = await builder.BuildWorkflowAsync<T>(cancellationToken); |
| | 2 | 56 | | return await RunAsync(workflowDefinition, options, cancellationToken); |
| | 2 | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <inheritdoc /> |
| | | 60 | | public async Task<TResult> RunAsync<T, TResult>(RunWorkflowOptions? options = null, CancellationToken cancellationTo |
| | | 61 | | { |
| | 0 | 62 | | var builder = workflowBuilderFactory.CreateBuilder(); |
| | 0 | 63 | | var workflow = await builder.BuildWorkflowAsync<T>(cancellationToken); |
| | 0 | 64 | | var result = await RunAsync(workflow, options, cancellationToken); |
| | 0 | 65 | | return (TResult)result.Result!; |
| | 0 | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <inheritdoc /> |
| | | 69 | | public async Task<RunWorkflowResult> RunAsync(WorkflowGraph workflowGraph, RunWorkflowOptions? options = null, Cance |
| | | 70 | | { |
| | | 71 | | // Set up a workflow execution context. |
| | 315 | 72 | | var instanceId = options?.WorkflowInstanceId ?? identityGenerator.GenerateId(); |
| | 315 | 73 | | var input = options?.Input; |
| | 315 | 74 | | var properties = options?.Properties; |
| | 315 | 75 | | var correlationId = options?.CorrelationId; |
| | 315 | 76 | | var triggerActivityId = options?.TriggerActivityId; |
| | 315 | 77 | | var parentWorkflowInstanceId = options?.ParentWorkflowInstanceId; |
| | 315 | 78 | | var workflowExecutionContext = await WorkflowExecutionContext.CreateAsync( |
| | 315 | 79 | | serviceProvider, |
| | 315 | 80 | | workflowGraph, |
| | 315 | 81 | | instanceId, |
| | 315 | 82 | | correlationId, |
| | 315 | 83 | | parentWorkflowInstanceId, |
| | 315 | 84 | | input, |
| | 315 | 85 | | properties, |
| | 315 | 86 | | null, |
| | 315 | 87 | | triggerActivityId, |
| | 315 | 88 | | cancellationToken); |
| | | 89 | | |
| | | 90 | | // Schedule the first activity. |
| | 315 | 91 | | workflowExecutionContext.ScheduleWorkflow(); |
| | | 92 | | |
| | 315 | 93 | | return await RunAsync(workflowExecutionContext); |
| | 315 | 94 | | } |
| | | 95 | | |
| | | 96 | | /// <inheritdoc /> |
| | | 97 | | public async Task<RunWorkflowResult> RunAsync(Workflow workflow, RunWorkflowOptions? options = null, CancellationTok |
| | | 98 | | { |
| | 2 | 99 | | var workflowGraph = await workflowGraphBuilder.BuildAsync(workflow, cancellationToken); |
| | 2 | 100 | | return await RunAsync(workflowGraph, options, cancellationToken); |
| | 2 | 101 | | } |
| | | 102 | | |
| | | 103 | | /// <inheritdoc /> |
| | | 104 | | public async Task<RunWorkflowResult> RunAsync(Workflow workflow, WorkflowState workflowState, RunWorkflowOptions? op |
| | | 105 | | { |
| | 2 | 106 | | var workflowGraph = await workflowGraphBuilder.BuildAsync(workflow, cancellationToken); |
| | 2 | 107 | | return await RunAsync(workflowGraph, workflowState, options, cancellationToken); |
| | 2 | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <inheritdoc /> |
| | | 111 | | public async Task<RunWorkflowResult> RunAsync(WorkflowGraph workflowGraph, WorkflowState workflowState, RunWorkflowO |
| | | 112 | | { |
| | | 113 | | // Create a workflow execution context. |
| | 121 | 114 | | var input = options?.Input; |
| | 121 | 115 | | var variables = options?.Variables; |
| | 121 | 116 | | var properties = options?.Properties; |
| | 121 | 117 | | var correlationId = options?.CorrelationId ?? workflowState.CorrelationId; |
| | 121 | 118 | | var triggerActivityId = options?.TriggerActivityId; |
| | 121 | 119 | | var parentWorkflowInstanceId = options?.ParentWorkflowInstanceId; |
| | 121 | 120 | | var workflowExecutionContext = await WorkflowExecutionContext.CreateAsync( |
| | 121 | 121 | | serviceProvider, |
| | 121 | 122 | | workflowGraph, |
| | 121 | 123 | | workflowState, |
| | 121 | 124 | | correlationId, |
| | 121 | 125 | | parentWorkflowInstanceId, |
| | 121 | 126 | | input, |
| | 121 | 127 | | properties, |
| | 121 | 128 | | null, |
| | 121 | 129 | | triggerActivityId, |
| | 121 | 130 | | cancellationToken); |
| | | 131 | | |
| | 121 | 132 | | var bookmarkId = options?.BookmarkId; |
| | 121 | 133 | | var activityHandle = options?.ActivityHandle; |
| | | 134 | | |
| | 121 | 135 | | if (!string.IsNullOrEmpty(bookmarkId)) |
| | | 136 | | { |
| | 72 | 137 | | var bookmark = workflowState.Bookmarks.FirstOrDefault(x => x.Id == bookmarkId); |
| | | 138 | | |
| | 36 | 139 | | if (bookmark != null) |
| | 36 | 140 | | workflowExecutionContext.ScheduleBookmark(bookmark); |
| | | 141 | | } |
| | 85 | 142 | | else if (activityHandle != null) |
| | | 143 | | { |
| | 0 | 144 | | if (!string.IsNullOrEmpty(activityHandle.ActivityInstanceId)) |
| | | 145 | | { |
| | 0 | 146 | | var activityExecutionContext = workflowExecutionContext.ActivityExecutionContexts.FirstOrDefault(x => x. |
| | 0 | 147 | | ?? throw new("No activity execution context found with the specified ID." |
| | 0 | 148 | | workflowExecutionContext.ScheduleActivityExecutionContext(activityExecutionContext); |
| | | 149 | | } |
| | | 150 | | else |
| | | 151 | | { |
| | 0 | 152 | | var activity = workflowExecutionContext.FindActivity(activityHandle); |
| | 0 | 153 | | if (activity != null) workflowExecutionContext.ScheduleActivity(activity); |
| | | 154 | | } |
| | | 155 | | } |
| | 85 | 156 | | else if (workflowExecutionContext.Scheduler.HasAny) |
| | | 157 | | { |
| | | 158 | | // Do nothing. The scheduler already has activities to schedule. |
| | | 159 | | } |
| | | 160 | | else |
| | | 161 | | { |
| | | 162 | | // Check if there are any interrupted activities. |
| | 85 | 163 | | var interruptedActivityExecutionContexts = workflowExecutionContext.ActivityExecutionContexts.Where(x => x.I |
| | | 164 | | |
| | 85 | 165 | | if (interruptedActivityExecutionContexts.Count > 0) |
| | | 166 | | { |
| | | 167 | | // Schedule the interrupted activities. |
| | 0 | 168 | | foreach (var pendingActivityExecutionContext in interruptedActivityExecutionContexts) |
| | 0 | 169 | | workflowExecutionContext.ScheduleActivityExecutionContext(pendingActivityExecutionContext); |
| | | 170 | | } |
| | | 171 | | else |
| | | 172 | | { |
| | | 173 | | // Nothing was scheduled. Schedule the workflow itself. |
| | 85 | 174 | | var vars = variables?.Select(x => new Variable(x.Key, x.Value)).ToList(); |
| | 85 | 175 | | workflowExecutionContext.ScheduleWorkflow(variables: vars); |
| | | 176 | | } |
| | | 177 | | } |
| | | 178 | | |
| | | 179 | | // Set variables, if any. |
| | 121 | 180 | | if (variables != null) |
| | | 181 | | { |
| | 0 | 182 | | var rootContext = workflowExecutionContext.ActivityExecutionContexts.FirstOrDefault(x => x.ParentActivityExe |
| | | 183 | | |
| | 0 | 184 | | if (rootContext != null) |
| | | 185 | | { |
| | 0 | 186 | | foreach (var variable in variables) |
| | 0 | 187 | | rootContext.SetDynamicVariable(variable.Key, variable.Value); |
| | | 188 | | } |
| | | 189 | | } |
| | | 190 | | |
| | 121 | 191 | | return await RunAsync(workflowExecutionContext); |
| | 121 | 192 | | } |
| | | 193 | | |
| | | 194 | | /// <inheritdoc /> |
| | | 195 | | public async Task<RunWorkflowResult> RunAsync(WorkflowExecutionContext workflowExecutionContext) |
| | | 196 | | { |
| | 436 | 197 | | var loggerState = loggerStateGenerator.GenerateLoggerState(workflowExecutionContext); |
| | 436 | 198 | | using var loggingScope = logger.BeginScope(loggerState); |
| | 436 | 199 | | var workflow = workflowExecutionContext.Workflow; |
| | 436 | 200 | | var cancellationToken = workflowExecutionContext.CancellationToken; |
| | | 201 | | |
| | 436 | 202 | | await notificationSender.SendAsync(new WorkflowExecuting(workflow, workflowExecutionContext), cancellationToken) |
| | | 203 | | |
| | | 204 | | // If the status is Pending, it means the workflow is started for the first time. |
| | 436 | 205 | | if (workflowExecutionContext.SubStatus == WorkflowSubStatus.Pending) |
| | | 206 | | { |
| | 400 | 207 | | workflowExecutionContext.TransitionTo(WorkflowSubStatus.Executing); |
| | 400 | 208 | | await notificationSender.SendAsync(new WorkflowStarted(workflow, workflowExecutionContext), cancellationToke |
| | | 209 | | } |
| | | 210 | | |
| | 436 | 211 | | await pipeline.ExecuteAsync(workflowExecutionContext); |
| | 436 | 212 | | var workflowState = workflowStateExtractor.Extract(workflowExecutionContext); |
| | | 213 | | |
| | 436 | 214 | | if (workflowState.Status == WorkflowStatus.Finished) |
| | | 215 | | { |
| | 392 | 216 | | await notificationSender.SendAsync(new WorkflowFinished(workflow, workflowState, workflowExecutionContext), |
| | | 217 | | } |
| | | 218 | | |
| | 436 | 219 | | var result = workflow.ResultVariable?.Get(workflowExecutionContext.MemoryRegister); |
| | 436 | 220 | | var workflowExecutionLogEntries = workflowExecutionContext.ExecutionLog.ToList(); |
| | 436 | 221 | | var activityExecutionContexts = workflowExecutionContext.ActivityExecutionContexts.ToList(); |
| | 436 | 222 | | var journal = new Journal(workflowExecutionLogEntries, activityExecutionContexts); |
| | 436 | 223 | | await notificationSender.SendAsync(new WorkflowExecuted(workflow, workflowState, workflowExecutionContext), canc |
| | 436 | 224 | | await commitStateHandler.CommitAsync(workflowExecutionContext, workflowState, cancellationToken); |
| | 436 | 225 | | return new(workflowExecutionContext, workflowState, workflowExecutionContext.Workflow, result, journal); |
| | 436 | 226 | | } |
| | | 227 | | } |