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