| | | 1 | | using Elsa.Extensions; |
| | | 2 | | using Elsa.Workflows.CommitStates; |
| | | 3 | | using Elsa.Workflows.Models; |
| | | 4 | | using Elsa.Workflows.Options; |
| | | 5 | | using Elsa.Workflows.Pipelines.WorkflowExecution; |
| | | 6 | | using Microsoft.Extensions.Options; |
| | | 7 | | |
| | | 8 | | namespace Elsa.Workflows.Middleware.Workflows; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Installs middleware that executes scheduled work items. |
| | | 12 | | /// </summary> |
| | | 13 | | public static class UseActivitySchedulerMiddlewareExtensions |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Installs middleware that executes scheduled work items. |
| | | 17 | | /// </summary> |
| | | 18 | | public static IWorkflowExecutionPipelineBuilder UseDefaultActivityScheduler(this IWorkflowExecutionPipelineBuilder p |
| | | 19 | | } |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// A workflow execution middleware component that executes scheduled work items. |
| | | 23 | | /// </summary> |
| | 501 | 24 | | public class DefaultActivitySchedulerMiddleware(WorkflowMiddlewareDelegate next, IActivityInvoker activityInvoker, IComm |
| | | 25 | | { |
| | | 26 | | /// <inheritdoc /> |
| | | 27 | | public override async ValueTask InvokeAsync(WorkflowExecutionContext context) |
| | | 28 | | { |
| | 509 | 29 | | var scheduler = context.Scheduler; |
| | | 30 | | |
| | 509 | 31 | | context.TransitionTo(WorkflowSubStatus.Executing); |
| | 509 | 32 | | await ConditionallyCommitStateAsync(context, WorkflowLifetimeEvent.WorkflowExecuting); |
| | | 33 | | |
| | 3825 | 34 | | while (scheduler.HasAny) |
| | | 35 | | { |
| | | 36 | | // Do not start a workflow if cancellation has been requested. |
| | 3316 | 37 | | if (context.CancellationToken.IsCancellationRequested) |
| | | 38 | | break; |
| | | 39 | | |
| | 3316 | 40 | | var currentWorkItem = scheduler.Take(); |
| | 3316 | 41 | | await ExecuteWorkItemAsync(context, currentWorkItem); |
| | | 42 | | } |
| | | 43 | | |
| | 509 | 44 | | await Next(context); |
| | | 45 | | |
| | 509 | 46 | | if (context.Status == WorkflowStatus.Running) |
| | 489 | 47 | | context.TransitionTo(context.AllActivitiesCompleted() ? WorkflowSubStatus.Finished : WorkflowSubStatus.Suspe |
| | 509 | 48 | | } |
| | | 49 | | |
| | | 50 | | private async Task ExecuteWorkItemAsync(WorkflowExecutionContext context, ActivityWorkItem workItem) |
| | | 51 | | { |
| | 3316 | 52 | | var options = new ActivityInvocationOptions |
| | 3316 | 53 | | { |
| | 3316 | 54 | | Owner = workItem.Owner, |
| | 3316 | 55 | | ExistingActivityExecutionContext = workItem.ExistingActivityExecutionContext, |
| | 3316 | 56 | | Tag = workItem.Tag, |
| | 3316 | 57 | | Variables = workItem.Variables, |
| | 3316 | 58 | | Input = workItem.Input |
| | 3316 | 59 | | }; |
| | | 60 | | |
| | 3316 | 61 | | await activityInvoker.InvokeAsync(context, workItem.Activity, options); |
| | 3316 | 62 | | } |
| | | 63 | | |
| | | 64 | | private async Task ConditionallyCommitStateAsync(WorkflowExecutionContext context, WorkflowLifetimeEvent lifetimeEve |
| | | 65 | | { |
| | 509 | 66 | | var strategyName = context.Workflow.Options.CommitStrategyName; |
| | 509 | 67 | | IWorkflowCommitStrategy? strategy = !string.IsNullOrWhiteSpace(strategyName) |
| | 509 | 68 | | ? commitStrategyRegistry.FindWorkflowStrategy(strategyName) |
| | 509 | 69 | | : commitStateOptions.Value.DefaultWorkflowCommitStrategy; |
| | | 70 | | |
| | 509 | 71 | | if (strategy == null) |
| | 509 | 72 | | return; |
| | | 73 | | |
| | 0 | 74 | | var strategyContext = new WorkflowCommitStateStrategyContext(context, lifetimeEvent); |
| | 0 | 75 | | var commitAction = strategy.ShouldCommit(strategyContext); |
| | | 76 | | |
| | 0 | 77 | | if (commitAction is CommitAction.Commit) |
| | 0 | 78 | | await context.CommitAsync(); |
| | 509 | 79 | | } |
| | | 80 | | } |