< 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>
 47223public class DefaultActivitySchedulerMiddleware(WorkflowMiddlewareDelegate next, IActivityInvoker activityInvoker, IComm
 24{
 25    /// <inheritdoc />
 26    public override async ValueTask InvokeAsync(WorkflowExecutionContext context)
 27    {
 48728        var scheduler = context.Scheduler;
 29
 48730        context.TransitionTo(WorkflowSubStatus.Executing);
 48731        await ConditionallyCommitStateAsync(context, WorkflowLifetimeEvent.WorkflowExecuting);
 32
 371333        while (scheduler.HasAny)
 34        {
 35            // Do not start a workflow if cancellation has been requested.
 322636            if (context.CancellationToken.IsCancellationRequested)
 37                break;
 38
 322639            var currentWorkItem = scheduler.Take();
 322640            await ExecuteWorkItemAsync(context, currentWorkItem);
 41        }
 42
 48743        await Next(context);
 44
 48745        if (context.Status == WorkflowStatus.Running)
 47346            context.TransitionTo(context.AllActivitiesCompleted() ? WorkflowSubStatus.Finished : WorkflowSubStatus.Suspe
 48747    }
 48
 49    private async Task ExecuteWorkItemAsync(WorkflowExecutionContext context, ActivityWorkItem workItem)
 50    {
 322651        var options = new ActivityInvocationOptions
 322652        {
 322653            Owner = workItem.Owner,
 322654            ExistingActivityExecutionContext = workItem.ExistingActivityExecutionContext,
 322655            Tag = workItem.Tag,
 322656            Variables = workItem.Variables,
 322657            Input = workItem.Input
 322658        };
 59
 322660        await activityInvoker.InvokeAsync(context, workItem.Activity, options);
 322661    }
 62
 63    private async Task ConditionallyCommitStateAsync(WorkflowExecutionContext context, WorkflowLifetimeEvent lifetimeEve
 64    {
 48765        var strategyName = context.Workflow.Options.CommitStrategyName;
 48766        var strategy = string.IsNullOrWhiteSpace(strategyName) ? null : commitStrategyRegistry.FindWorkflowStrategy(stra
 67
 48768        if(strategy == null)
 48769            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();
 48776    }
 77}