< Summary

Information
Class: Elsa.Scheduling.Services.DefaultTriggerScheduler
Assembly: Elsa.Scheduling
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Scheduling/Services/DefaultTriggerScheduler.cs
Line coverage
70%
Covered lines: 40
Uncovered lines: 17
Coverable lines: 57
Total lines: 105
Line coverage: 70.1%
Branch coverage
66%
Covered branches: 8
Total branches: 12
Branch coverage: 66.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
ScheduleAsync()60%141064.58%
UnscheduleAsync()100%22100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Scheduling/Services/DefaultTriggerScheduler.cs

#LineLine coverage
 1using Elsa.Common;
 2using Elsa.Extensions;
 3using Elsa.Scheduling.Activities;
 4using Elsa.Scheduling.Bookmarks;
 5using Elsa.Workflows.Models;
 6using Elsa.Workflows.Runtime.Entities;
 7using Microsoft.Extensions.Logging;
 8
 9namespace Elsa.Scheduling.Services;
 10
 11/// <summary>
 12/// A default implementation of <see cref="ITriggerScheduler"/> that schedules triggers using <see cref="IWorkflowSchedu
 13/// </summary>
 45314public class DefaultTriggerScheduler(IWorkflowScheduler workflowScheduler, ISystemClock systemClock, ILogger<DefaultTrig
 15    : ITriggerScheduler
 16{
 17    /// <inheritdoc />
 18    public async Task ScheduleAsync(IEnumerable<StoredTrigger> triggers, CancellationToken cancellationToken = default)
 19    {
 63520        var triggerList = triggers.ToList();
 63521        var timerTriggers = triggerList.Filter<Activities.Timer>();
 63522        var startAtTriggers = triggerList.Filter<StartAt>();
 63523        var cronTriggers = triggerList.Filter<Cron>();
 63524        var now = systemClock.UtcNow;
 25
 26        // Schedule each Timer trigger.
 127427        foreach (var trigger in timerTriggers)
 28        {
 229            var (startAt, interval) = trigger.GetPayload<TimerTriggerPayload>();
 230            var input = new { StartAt = startAt, Interval = interval }.ToDictionary();
 231            var request = new ScheduleNewWorkflowInstanceRequest
 232            {
 233                WorkflowDefinitionHandle = WorkflowDefinitionHandle.ByDefinitionVersionId(trigger.WorkflowDefinitionVers
 234                TriggerActivityId = trigger.ActivityId,
 235                Input = input
 236            };
 237            await workflowScheduler.ScheduleRecurringAsync(trigger.Id, request, startAt, interval, cancellationToken);
 38        }
 39
 40        // Schedule each StartAt trigger.
 127041        foreach (var trigger in startAtTriggers)
 42        {
 043            var executeAt = trigger.GetPayload<StartAtPayload>().ExecuteAt;
 44
 45            // If the trigger is in the past, log info and skip scheduling.
 046            if (executeAt < now)
 47            {
 048                logger.LogInformation("StartAt trigger is in the past. TriggerId: {TriggerId}. ExecuteAt: {ExecuteAt}. S
 049                continue;
 50            }
 51
 052            var input = new { ExecuteAt = executeAt }.ToDictionary();
 053            var request = new ScheduleNewWorkflowInstanceRequest
 054            {
 055                WorkflowDefinitionHandle = WorkflowDefinitionHandle.ByDefinitionVersionId(trigger.WorkflowDefinitionVers
 056                TriggerActivityId = trigger.ActivityId,
 057                Input = input
 058            };
 59
 060            await workflowScheduler.ScheduleAtAsync(trigger.Id, request, executeAt, cancellationToken);
 61        }
 62
 63        // Schedule each Cron trigger.
 127264        foreach (var trigger in cronTriggers)
 65        {
 166            var payload = trigger.GetPayload<CronTriggerPayload>();
 167            var cronExpression = payload.CronExpression;
 68
 169            if (string.IsNullOrWhiteSpace(cronExpression))
 70            {
 071                logger.LogWarning("Cron expression is empty. TriggerId: {TriggerId}. Skipping scheduling of this trigger
 072                continue;
 73            }
 74
 175            var input = new { CronExpression = cronExpression }.ToDictionary();
 176            var request = new ScheduleNewWorkflowInstanceRequest
 177            {
 178                WorkflowDefinitionHandle = WorkflowDefinitionHandle.ByDefinitionVersionId(trigger.WorkflowDefinitionVers
 179                TriggerActivityId = trigger.ActivityId,
 180                Input = input
 181            };
 82            try
 83            {
 184                await workflowScheduler.ScheduleCronAsync(trigger.Id, request, cronExpression, cancellationToken);
 185            }
 086            catch (FormatException ex)
 87            {
 088                logger.LogWarning(ex, "Cron expression format error. CronExpression: {CronExpression}", cronExpression);
 089            }
 190        }
 63591    }
 92
 93    /// <inheritdoc />
 94    public async Task UnscheduleAsync(IEnumerable<StoredTrigger> triggers, CancellationToken cancellationToken = default
 95    {
 63796        var triggerList = triggers.ToList();
 63797        var timerTriggers = triggerList.Filter<Activities.Timer>();
 63798        var startAtTriggers = triggerList.Filter<StartAt>();
 63799        var cronTriggers = triggerList.Filter<Cron>();
 637100        var filteredTriggers = timerTriggers.Concat(startAtTriggers).Concat(cronTriggers);
 101
 1276102        foreach (var trigger in filteredTriggers)
 1103            await workflowScheduler.UnscheduleAsync(trigger.Id, cancellationToken);
 637104    }
 105}