| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | using Elsa.Expressions.Models; |
| | | 3 | | using Elsa.Extensions; |
| | | 4 | | using Elsa.Workflows; |
| | | 5 | | |
| | | 6 | | namespace Elsa.Scheduling.Activities; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// </summary> |
| | 31 | 10 | | public abstract class TimerBase([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : EventGene |
| | | 11 | | { |
| | | 12 | | protected abstract TimeSpan GetInterval(ExpressionExecutionContext context); |
| | | 13 | | |
| | | 14 | | protected virtual ValueTask OnTimerElapsedAsync(ActivityExecutionContext context) |
| | | 15 | | { |
| | 2 | 16 | | OnTimerElapsed(context); |
| | 2 | 17 | | return default; |
| | | 18 | | } |
| | | 19 | | |
| | | 20 | | protected virtual void OnTimerElapsed(ActivityExecutionContext context) |
| | | 21 | | { |
| | 2 | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <inheritdoc /> |
| | | 25 | | protected override void Execute(ActivityExecutionContext context) |
| | | 26 | | { |
| | 7 | 27 | | var interval = GetInterval(context.ExpressionExecutionContext); |
| | 7 | 28 | | context.RepeatWithInterval(interval, TimerElapsedAsync); |
| | 7 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <inheritdoc /> |
| | | 32 | | protected override object GetTriggerPayload(TriggerIndexingContext context) |
| | | 33 | | { |
| | 4 | 34 | | var interval = GetInterval(context.ExpressionExecutionContext); |
| | 4 | 35 | | return context.GetTimerTriggerStimulus(interval); |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | private async ValueTask TimerElapsedAsync(ActivityExecutionContext context) |
| | | 39 | | { |
| | 2 | 40 | | await OnTimerElapsedAsync(context); |
| | 2 | 41 | | await context.CompleteActivityAsync(); |
| | 2 | 42 | | } |
| | | 43 | | } |