< 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: 77
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;
 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>
 42517    public static IWorkflowExecutionPipelineBuilder UseDefaultActivityScheduler(this IWorkflowExecutionPipelineBuilder p
 18}
 19
 20/// <summary>
 21/// A workflow execution middleware component that executes scheduled work items.
 22/// </summary>
 23public 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}