elsa-core/src/modules/Elsa.Modules.Scheduling/Triggers/Timer.cs

43 lines
1.2 KiB
C#
Raw Normal View History

2022-01-04 08:42:12 +00:00
using System;
using System.Collections.Generic;
2022-03-15 12:01:10 +00:00
using System.Text.Json.Serialization;
2022-01-04 08:42:12 +00:00
using Elsa.Attributes;
using Elsa.Contracts;
2022-01-04 08:42:12 +00:00
using Elsa.Models;
2022-02-01 19:45:11 +00:00
namespace Elsa.Modules.Scheduling.Triggers;
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-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-03-07 11:01:27 +00:00
protected override IEnumerable<object> GetTriggerData(TriggerIndexingContext context)
{
var interval = context.ExpressionExecutionContext.Get(Interval);
var clock = context.ExpressionExecutionContext.GetRequiredService<ISystemClock>();
var executeAt = clock.UtcNow.Add(interval);
2022-01-14 12:54:17 +00:00
yield 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);