| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | using Elsa.Extensions; |
| | | 3 | | using Elsa.Scheduling.Bookmarks; |
| | | 4 | | using Elsa.Workflows; |
| | | 5 | | using Elsa.Workflows.Attributes; |
| | | 6 | | using Elsa.Workflows.Models; |
| | | 7 | | |
| | | 8 | | namespace Elsa.Scheduling.Activities; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Represents a timer to periodically trigger the workflow. |
| | | 12 | | /// </summary> |
| | | 13 | | [Activity("Elsa", "Scheduling", "Trigger workflow execution at a specific interval using a CRON expression.")] |
| | | 14 | | public class Cron : EventGenerator |
| | | 15 | | { |
| | | 16 | | /// <inheritdoc /> |
| | 32 | 17 | | public Cron([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, line) |
| | | 18 | | { |
| | 32 | 19 | | } |
| | | 20 | | |
| | | 21 | | /// <inheritdoc /> |
| | 24 | 22 | | public Cron(string cronExpression, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : th |
| | | 23 | | { |
| | 24 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <inheritdoc /> |
| | 24 | 27 | | public Cron(Input<string> cronExpression, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = nul |
| | | 28 | | { |
| | 24 | 29 | | CronExpression = cronExpression; |
| | 24 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// The interval at which the timer should execute. |
| | | 34 | | /// </summary> |
| | | 35 | | [Input(Description = "The CRON expression at which the timer should execute.")] |
| | 116 | 36 | | public Input<string> CronExpression { get; set; } = null!; |
| | | 37 | | |
| | | 38 | | /// <inheritdoc /> |
| | | 39 | | protected override async ValueTask ExecuteAsync(ActivityExecutionContext context) |
| | | 40 | | { |
| | 7 | 41 | | if(context.IsTriggerOfWorkflow()) |
| | | 42 | | { |
| | 1 | 43 | | await context.CompleteActivityAsync(); |
| | 1 | 44 | | return; |
| | | 45 | | } |
| | | 46 | | |
| | 6 | 47 | | var cronParser = context.GetRequiredService<ICronParser>(); |
| | 6 | 48 | | var cronExpression = context.ExpressionExecutionContext.Get(CronExpression)!; |
| | 6 | 49 | | var executeAt = cronParser.GetNextOccurrence(cronExpression); |
| | | 50 | | |
| | 6 | 51 | | context.JournalData.Add("ExecuteAt", executeAt); |
| | 6 | 52 | | context.CreateBookmark(new CronBookmarkPayload(executeAt, cronExpression)); |
| | 7 | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <inheritdoc /> |
| | | 56 | | protected override object GetTriggerPayload(TriggerIndexingContext context) |
| | | 57 | | { |
| | 6 | 58 | | var cronExpression = context.ExpressionExecutionContext.Get(CronExpression)!; |
| | 6 | 59 | | return new CronTriggerPayload(cronExpression); |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Creates a new <see cref="Cron"/> activity set to trigger at the specified cron expression. |
| | | 64 | | /// </summary> |
| | 24 | 65 | | public static Cron FromCronExpression(string value, [CallerFilePath] string? source = null, [CallerLineNumber] int? |
| | | 66 | | } |