< Summary

Information
Class: Elsa.Workflows.Middleware.Workflows.DefaultActivitySchedulerMiddleware
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Middleware/Workflows/DefaultActivitySchedulerMiddleware.cs
Line coverage
87%
Covered lines: 29
Uncovered lines: 4
Coverable lines: 33
Total lines: 80
Line coverage: 87.8%
Branch coverage
71%
Covered branches: 10
Total branches: 14
Branch coverage: 71.4%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
InvokeAsync()100%88100%
ExecuteWorkItemAsync()100%11100%
ConditionallyCommitStateAsync()33.33%8663.63%

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>
 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>
 50124public class DefaultActivitySchedulerMiddleware(WorkflowMiddlewareDelegate next, IActivityInvoker activityInvoker, IComm
 25{
 26    /// <inheritdoc />
 27    public override async ValueTask InvokeAsync(WorkflowExecutionContext context)
 28    {
 50929        var scheduler = context.Scheduler;
 30
 50931        context.TransitionTo(WorkflowSubStatus.Executing);
 50932        await ConditionallyCommitStateAsync(context, WorkflowLifetimeEvent.WorkflowExecuting);
 33
 382534        while (scheduler.HasAny)
 35        {
 36            // Do not start a workflow if cancellation has been requested.
 331637            if (context.CancellationToken.IsCancellationRequested)
 38                break;
 39
 331640            var currentWorkItem = scheduler.Take();
 331641            await ExecuteWorkItemAsync(context, currentWorkItem);
 42        }
 43
 50944        await Next(context);
 45
 50946        if (context.Status == WorkflowStatus.Running)
 48947            context.TransitionTo(context.AllActivitiesCompleted() ? WorkflowSubStatus.Finished : WorkflowSubStatus.Suspe
 50948    }
 49
 50    private async Task ExecuteWorkItemAsync(WorkflowExecutionContext context, ActivityWorkItem workItem)
 51    {
 331652        var options = new ActivityInvocationOptions
 331653        {
 331654            Owner = workItem.Owner,
 331655            ExistingActivityExecutionContext = workItem.ExistingActivityExecutionContext,
 331656            Tag = workItem.Tag,
 331657            Variables = workItem.Variables,
 331658            Input = workItem.Input
 331659        };
 60
 331661        await activityInvoker.InvokeAsync(context, workItem.Activity, options);
 331662    }
 63
 64    private async Task ConditionallyCommitStateAsync(WorkflowExecutionContext context, WorkflowLifetimeEvent lifetimeEve
 65    {
 50966        var strategyName = context.Workflow.Options.CommitStrategyName;
 50967        IWorkflowCommitStrategy? strategy = !string.IsNullOrWhiteSpace(strategyName)
 50968            ? commitStrategyRegistry.FindWorkflowStrategy(strategyName)
 50969            : commitStateOptions.Value.DefaultWorkflowCommitStrategy;
 70
 50971        if (strategy == null)
 50972            return;
 73
 074        var strategyContext = new WorkflowCommitStateStrategyContext(context, lifetimeEvent);
 075        var commitAction = strategy.ShouldCommit(strategyContext);
 76
 077        if (commitAction is CommitAction.Commit)
 078            await context.CommitAsync();
 50979    }
 80}