elsa-core/src/modules/Elsa.Scheduling/Activities/Timer.cs

43 lines
1.3 KiB
C#
Raw Normal View History

2022-11-13 11:45:34 +00:00
using System.Text.Json.Serialization;
using Elsa.Common.Services;
using Elsa.Workflows.Core;
using Elsa.Workflows.Core.Attributes;
using Elsa.Workflows.Core.Models;
2022-01-04 08:42:12 +00:00
namespace Elsa.Scheduling.Activities;
2022-01-04 08:42:12 +00:00
/// <summary>
2022-03-07 11:01:27 +00:00
/// Represents a timer to periodically trigger the workflow.
/// </summary>
2022-03-24 11:29:39 +00:00
[Activity( "Elsa", "Scheduling", "Trigger workflow execution at a specific interval.")]
2022-03-07 11:01:27 +00:00
public class Timer : EventGenerator
2022-01-04 08:42:12 +00:00
{
2022-03-15 12:01:10 +00:00
[JsonConstructor]
public Timer()
{
}
public Timer(TimeSpan interval) : this(new Input<TimeSpan>(interval))
{
}
public Timer(Input<TimeSpan> interval)
{
Interval = interval;
}
2022-01-04 08:42:12 +00:00
[Input] public Input<TimeSpan> Interval { get; set; } = default!;
2022-08-10 14:02:53 +00:00
protected override object GetTriggerPayload(TriggerIndexingContext context)
{
var interval = context.ExpressionExecutionContext.Get(Interval);
var clock = context.ExpressionExecutionContext.GetRequiredService<ISystemClock>();
var executeAt = clock.UtcNow.Add(interval);
return new TimerPayload(executeAt, interval);
}
2022-03-15 12:01:10 +00:00
public static Timer FromTimeSpan(TimeSpan value) => new(value);
public static Timer FromSeconds(double value) => FromTimeSpan(TimeSpan.FromSeconds(value));
2022-01-14 12:54:17 +00:00
}
2022-03-07 21:56:16 +00:00
public record TimerPayload(DateTimeOffset StartAt, TimeSpan Interval);