using System.Text.Json.Serialization; using Elsa.Common.Services; using Elsa.Workflows.Core; using Elsa.Workflows.Core.Attributes; using Elsa.Workflows.Core.Models; namespace Elsa.Scheduling.Activities; /// /// Represents a timer to periodically trigger the workflow. /// [Activity( "Elsa", "Scheduling", "Trigger workflow execution at a specific interval.")] public class Timer : EventGenerator { [JsonConstructor] public Timer() { } public Timer(TimeSpan interval) : this(new Input(interval)) { } public Timer(Input interval) { Interval = interval; } [Input] public Input Interval { get; set; } = default!; protected override object GetTriggerPayload(TriggerIndexingContext context) { var interval = context.ExpressionExecutionContext.Get(Interval); var clock = context.ExpressionExecutionContext.GetRequiredService(); var executeAt = clock.UtcNow.Add(interval); return new TimerPayload(executeAt, interval); } public static Timer FromTimeSpan(TimeSpan value) => new(value); public static Timer FromSeconds(double value) => FromTimeSpan(TimeSpan.FromSeconds(value)); } public record TimerPayload(DateTimeOffset StartAt, TimeSpan Interval);