| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | using Elsa.Common; |
| | | 3 | | using Elsa.Expressions.Models; |
| | | 4 | | using Elsa.Extensions; |
| | | 5 | | using Elsa.Scheduling.Bookmarks; |
| | | 6 | | using Elsa.Workflows; |
| | | 7 | | using Elsa.Workflows.Attributes; |
| | | 8 | | using Elsa.Workflows.Memory; |
| | | 9 | | using Elsa.Workflows.Models; |
| | | 10 | | using Microsoft.Extensions.Logging; |
| | | 11 | | |
| | | 12 | | namespace Elsa.Scheduling.Activities; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Triggers the workflow at a specific future timestamp. |
| | | 16 | | /// </summary> |
| | | 17 | | [Activity("Elsa", "Scheduling", "Trigger execution at a specific time in the future.")] |
| | | 18 | | public class StartAt : Trigger |
| | | 19 | | { |
| | | 20 | | private const string InputKey = "ExecuteAt"; |
| | | 21 | | |
| | | 22 | | /// <inheritdoc /> |
| | 23 | 23 | | public StartAt([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, line) |
| | | 24 | | { |
| | 23 | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <inheritdoc /> |
| | 0 | 28 | | public StartAt(Input<DateTimeOffset> dateTime, [CallerFilePath] string? source = null, [CallerLineNumber] int? line |
| | | 29 | | |
| | | 30 | | /// <inheritdoc /> |
| | | 31 | | public StartAt(Func<ExpressionExecutionContext, DateTimeOffset> dateTime, [CallerFilePath] string? source = null, [C |
| | 0 | 32 | | : this(new Input<DateTimeOffset>(dateTime), source, line) |
| | | 33 | | { |
| | 0 | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <inheritdoc /> |
| | | 37 | | public StartAt( |
| | | 38 | | Func<ExpressionExecutionContext, ValueTask<DateTimeOffset>> dateTime, |
| | 0 | 39 | | [CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : this(new Input<DateTimeOffset>(da |
| | | 40 | | { |
| | 0 | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <inheritdoc /> |
| | | 44 | | public StartAt(Func<ValueTask<DateTimeOffset>> dateTime, [CallerFilePath] string? source = null, [CallerLineNumber] |
| | 0 | 45 | | : this(new Input<DateTimeOffset>(dateTime), source, line) |
| | | 46 | | { |
| | 0 | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <inheritdoc /> |
| | | 50 | | public StartAt(Func<DateTimeOffset> dateTime, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = |
| | 0 | 51 | | : this(new Input<DateTimeOffset>(dateTime), source, line) |
| | | 52 | | { |
| | 0 | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <inheritdoc /> |
| | 23 | 56 | | public StartAt(DateTimeOffset dateTime, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) |
| | 23 | 57 | | DateTime = new Input<DateTimeOffset>(dateTime); |
| | | 58 | | |
| | | 59 | | /// <inheritdoc /> |
| | 0 | 60 | | public StartAt(Variable<DateTimeOffset> dateTime, [CallerFilePath] string? source = null, [CallerLineNumber] int? li |
| | 0 | 61 | | DateTime = new Input<DateTimeOffset>(dateTime); |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// The timestamp at which the workflow should be triggered. |
| | | 65 | | /// </summary> |
| | | 66 | | [Input] |
| | 84 | 67 | | public Input<DateTimeOffset> DateTime { get; set; } = null!; |
| | | 68 | | |
| | | 69 | | /// <inheritdoc /> |
| | | 70 | | protected override object GetTriggerPayload(TriggerIndexingContext context) |
| | | 71 | | { |
| | 0 | 72 | | var executeAt = context.ExpressionExecutionContext.Get(DateTime); |
| | 0 | 73 | | return new StartAtPayload(executeAt); |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <inheritdoc /> |
| | | 77 | | protected override async ValueTask ExecuteAsync(ActivityExecutionContext context) |
| | | 78 | | { |
| | 6 | 79 | | if (context.IsTriggerOfWorkflow()) |
| | | 80 | | { |
| | 1 | 81 | | await context.CompleteActivityAsync(); |
| | 1 | 82 | | return; |
| | | 83 | | } |
| | | 84 | | |
| | 5 | 85 | | var executeAt = context.ExpressionExecutionContext.Get(DateTime); |
| | 5 | 86 | | var clock = context.ExpressionExecutionContext.GetRequiredService<ISystemClock>(); |
| | 5 | 87 | | var now = clock.UtcNow; |
| | 5 | 88 | | var logger = context.GetRequiredService<ILogger<StartAt>>(); |
| | | 89 | | |
| | 5 | 90 | | context.JournalData.Add("Executed At", now); |
| | | 91 | | |
| | 5 | 92 | | if (executeAt <= now) |
| | | 93 | | { |
| | 2 | 94 | | logger.LogDebug("Scheduled trigger time lies in the past ('{Delta}'). Completing immediately", now - execute |
| | 2 | 95 | | await context.CompleteActivityAsync(); |
| | 2 | 96 | | return; |
| | | 97 | | } |
| | | 98 | | |
| | 3 | 99 | | var payload = new StartAtPayload(executeAt); |
| | 3 | 100 | | context.CreateBookmark(payload); |
| | 6 | 101 | | } |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// Creates a new <see cref="StartAt"/> activity set to trigger at the specified timestamp. |
| | | 105 | | /// </summary> |
| | 0 | 106 | | public static StartAt From(DateTimeOffset value, [CallerFilePath] string? source = null, [CallerLineNumber] int? lin |
| | | 107 | | } |