< Summary

Information
Class: Elsa.Workflows.Middleware.Workflows.UseActivitySchedulerMiddlewareExtensions
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Middleware/Workflows/DefaultActivitySchedulerMiddleware.cs
Line coverage
100%
Covered lines: 1
Uncovered lines: 0
Coverable lines: 1
Total lines: 80
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
UseDefaultActivityScheduler(...)100%11100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Middleware/Workflows/DefaultActivitySchedulerMiddleware.cs

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