< Summary

Information
Class: Elsa.Workflows.CommitStates.Strategies.PeriodicWorkflowStrategy
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/CommitStates/Strategies/Workflows/PeriodicWorkflowStrategy.cs
Line coverage
22%
Covered lines: 2
Uncovered lines: 7
Coverable lines: 9
Total lines: 29
Line coverage: 22.2%
Branch coverage
0%
Covered branches: 0
Total branches: 8
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
.cctor()100%210%
get_Interval()100%11100%
ShouldCommit(...)0%7280%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/CommitStates/Strategies/Workflows/PeriodicWorkflowStrategy.cs

#LineLine coverage
 1using System.ComponentModel;
 2using Elsa.Common;
 3
 4namespace Elsa.Workflows.CommitStates.Strategies;
 5
 6/// <summary>
 7/// Implements a periodic workflow commit strategy based on a specified time interval.
 8/// This strategy determines if a workflow should commit by comparing the elapsed time
 9/// since the last commit with the configured interval.
 10/// </summary>
 11[DisplayName("Commit Periodically")]
 12[Description("Determines whether a workflow state should be committed based on a specified time interval.")]
 113public class PeriodicWorkflowStrategy(TimeSpan interval) : IWorkflowCommitStrategy
 14{
 015    private static readonly object LastCommitPropertyKey = new();
 116    public TimeSpan Interval { get; } = interval;
 17
 18    public CommitAction ShouldCommit(WorkflowCommitStateStrategyContext context)
 19    {
 020        var lastCommit = context.WorkflowExecutionContext.TransientProperties.TryGetValue(LastCommitPropertyKey, out var
 021        var now = context.WorkflowExecutionContext.GetRequiredService<ISystemClock>().UtcNow;
 022        var shouldCommit = lastCommit == null || (now - lastCommit.Value).TotalMilliseconds > Interval.TotalMilliseconds
 23
 024        if (shouldCommit)
 025            context.WorkflowExecutionContext.TransientProperties[LastCommitPropertyKey] = now;
 26
 027        return shouldCommit ? CommitAction.Commit : CommitAction.Default;
 28    }
 29}