| | | 1 | | using Anichron.Core.Data.Repository; |
| | | 2 | | using Anichron.Core.Work; |
| | | 3 | | using Anichron.Worker.Settings; |
| | | 4 | | using Microsoft.Extensions.Options; |
| | | 5 | | using NodaTime; |
| | | 6 | | |
| | | 7 | | namespace Anichron.Worker.Work; |
| | | 8 | | |
| | | 9 | | /// <summary>One schedule per active storage config the Worker serves, at the crawl interval.</summary> |
| | | 10 | | /// <typeparam name="TPayload">The per-config kind scheduled.</typeparam> |
| | | 11 | | /// <param name="workerState">Names the user a per-user Worker serves.</param> |
| | | 12 | | /// <param name="options">Supplies the crawl interval.</param> |
| | 7 | 13 | | public sealed class StorageConfigSchedules<TPayload>(WorkerState workerState, IOptions<WorkerSettings> options) |
| | | 14 | | : IWorkScheduleSource |
| | | 15 | | where TPayload : IStorageConfigPayload<TPayload> |
| | | 16 | | { |
| | | 17 | | public async Task<IReadOnlyList<IWorkSchedule>> SchedulesAsync(IServiceProvider services, CancellationToken ct) |
| | 4 | 18 | | { |
| | 4 | 19 | | var repository = services.GetRequiredService<IUserStorageConfigRepository>(); |
| | 4 | 20 | | var served = workerState.ResolvedUserId is { } userId |
| | 4 | 21 | | ? await repository.GetActiveByUserIdAsync(userId, ct) |
| | 4 | 22 | | : await repository.GetAllActiveAsync(ct); |
| | 4 | 23 | | var interval = Duration.FromHours(options.Value.CrawlIntervalHours); |
| | 4 | 24 | | return [.. served.Select(config => new WorkSchedule<TPayload>(TPayload.For(config.Id), interval))]; |
| | 4 | 25 | | } |
| | | 26 | | } |