| | | 1 | | using Elsa.Common; |
| | | 2 | | using Elsa.Scheduling; |
| | | 3 | | using Elsa.Scheduling.Bookmarks; |
| | | 4 | | using Elsa.Workflows; |
| | | 5 | | using Elsa.Workflows.Models; |
| | | 6 | | |
| | | 7 | | // ReSharper disable once CheckNamespace |
| | | 8 | | namespace Elsa.Extensions; |
| | | 9 | | |
| | | 10 | | public static class TimerActivityExecutionContextExtensions |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Repeats the execution of the current activity at the specified interval. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <param name="context">The execution context of the activity.</param> |
| | | 16 | | /// <param name="interval">The time interval after which the activity should be repeated.</param> |
| | | 17 | | /// <param name="callback">An optional callback to execute immediately if the workflow is triggered.</param> |
| | | 18 | | public static void RepeatWithInterval(this ActivityExecutionContext context, TimeSpan interval, ExecuteActivityDeleg |
| | | 19 | | { |
| | 12 | 20 | | if(context.IsTriggerOfWorkflow()) |
| | | 21 | | { |
| | 2 | 22 | | callback?.Invoke(context); |
| | 2 | 23 | | return; |
| | | 24 | | } |
| | | 25 | | |
| | 10 | 26 | | var clock = context.ExpressionExecutionContext.GetRequiredService<ISystemClock>(); |
| | 10 | 27 | | var resumeAt = clock.UtcNow.Add(interval); |
| | | 28 | | |
| | 10 | 29 | | var bookmarkOptions = new CreateBookmarkArgs |
| | 10 | 30 | | { |
| | 10 | 31 | | BookmarkName = SchedulingStimulusNames.Timer, |
| | 10 | 32 | | Stimulus = new TimerBookmarkPayload(resumeAt), |
| | 10 | 33 | | Callback = callback |
| | 10 | 34 | | }; |
| | 10 | 35 | | context.CreateBookmark(bookmarkOptions); |
| | 10 | 36 | | } |
| | | 37 | | |
| | | 38 | | public static TimerTriggerPayload GetTimerTriggerStimulus(this TriggerIndexingContext context, TimeSpan interval) |
| | | 39 | | { |
| | 4 | 40 | | var clock = context.ExpressionExecutionContext.GetRequiredService<ISystemClock>(); |
| | 4 | 41 | | var executeAt = clock.UtcNow.Add(interval); |
| | 4 | 42 | | context.TriggerName = SchedulingStimulusNames.Timer; |
| | 4 | 43 | | return new(executeAt, interval); |
| | | 44 | | } |
| | | 45 | | } |