| | | 1 | | using Anichron.Core.Work; |
| | | 2 | | using NodaTime; |
| | | 3 | | |
| | | 4 | | namespace Anichron.Worker.Work; |
| | | 5 | | |
| | | 6 | | public interface IWorkSchedule |
| | | 7 | | { |
| | | 8 | | string DedupKey { get; } |
| | | 9 | | |
| | | 10 | | Duration Interval { get; } |
| | | 11 | | |
| | | 12 | | Task EnqueueAsync(IWorkQueue queue, CancellationToken ct); |
| | | 13 | | } |
| | | 14 | | |
| | | 15 | | /// <summary>The periodic schedules a Worker serves, asked again at every scheduler tick.</summary> |
| | | 16 | | public interface IWorkScheduleSource |
| | | 17 | | { |
| | | 18 | | Task<IReadOnlyList<IWorkSchedule>> SchedulesAsync(IServiceProvider services, CancellationToken ct); |
| | | 19 | | } |
| | | 20 | | |
| | 21 | 21 | | public class WorkSchedule<TPayload>(TPayload payload, Duration interval) : IWorkSchedule, IWorkScheduleSource |
| | | 22 | | where TPayload : IWorkPayload |
| | | 23 | | { |
| | | 24 | | public string DedupKey { get; } = payload.DedupKey |
| | | 25 | | ?? throw new InvalidOperationException($"A periodic {TPayload.Kind} item needs a dedup key."); |
| | | 26 | | |
| | 18 | 27 | | public Duration Interval => interval; |
| | | 28 | | |
| | 6 | 29 | | public Task EnqueueAsync(IWorkQueue queue, CancellationToken ct) => queue.EnqueueAsync(payload, null, ct); |
| | | 30 | | |
| | | 31 | | public Task<IReadOnlyList<IWorkSchedule>> SchedulesAsync(IServiceProvider services, CancellationToken ct) |
| | 7 | 32 | | => Task.FromResult<IReadOnlyList<IWorkSchedule>>([this]); |
| | | 33 | | } |