< 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: 27
Uncovered lines: 4
Coverable lines: 31
Total lines: 77
Line coverage: 87%
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%9655.55%

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;
 6
 7namespace Elsa.Workflows.Middleware.Workflows;
 8
 9/// <summary>
 10/// Installs middleware that executes scheduled work items.
 11/// </summary>
 12public static class UseActivitySchedulerMiddlewareExtensions
 13{
 14    /// <summary>
 15    /// Installs middleware that executes scheduled work items.
 16    /// </summary>
 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>
 42123public class DefaultActivitySchedulerMiddleware(WorkflowMiddlewareDelegate next, IActivityInvoker activityInvoker, IComm
 24{
 25    /// <inheritdoc />
 26    public override async ValueTask InvokeAsync(WorkflowExecutionContext context)
 27    {
 43628        var scheduler = context.Scheduler;
 29
 43630        context.TransitionTo(WorkflowSubStatus.Executing);
 43631        await ConditionallyCommitStateAsync(context, WorkflowLifetimeEvent.WorkflowExecuting);
 32
 345533        while (scheduler.HasAny)
 34        {
 35            // Do not start a workflow if cancellation has been requested.
 301936            if (context.CancellationToken.IsCancellationRequested)
 37                break;
 38
 301939            var currentWorkItem = scheduler.Take();
 301940            await ExecuteWorkItemAsync(context, currentWorkItem);
 41        }
 42
 43643        await Next(context);
 44
 43645        if (context.Status == WorkflowStatus.Running)
 42246            context.TransitionTo(context.AllActivitiesCompleted() ? WorkflowSubStatus.Finished : WorkflowSubStatus.Suspe
 43647    }
 48
 49    private async Task ExecuteWorkItemAsync(WorkflowExecutionContext context, ActivityWorkItem workItem)
 50    {
 301951        var options = new ActivityInvocationOptions
 301952        {
 301953            Owner = workItem.Owner,
 301954            ExistingActivityExecutionContext = workItem.ExistingActivityExecutionContext,
 301955            Tag = workItem.Tag,
 301956            Variables = workItem.Variables,
 301957            Input = workItem.Input
 301958        };
 59
 301960        await activityInvoker.InvokeAsync(context, workItem.Activity, options);
 301961    }
 62
 63    private async Task ConditionallyCommitStateAsync(WorkflowExecutionContext context, WorkflowLifetimeEvent lifetimeEve
 64    {
 43665        var strategyName = context.Workflow.Options.CommitStrategyName;
 43666        var strategy = string.IsNullOrWhiteSpace(strategyName) ? null : commitStrategyRegistry.FindWorkflowStrategy(stra
 67
 43668        if(strategy == null)
 43669            return;
 70
 071        var strategyContext = new WorkflowCommitStateStrategyContext(context, lifetimeEvent);
 072        var commitAction = strategy.ShouldCommit(strategyContext);
 73
 074        if (commitAction is CommitAction.Commit)
 075            await context.CommitAsync();
 43676    }
 77}