| | | 1 | | using Anichron.Core.Data.Repository; |
| | | 2 | | using Anichron.Core.Domain; |
| | | 3 | | using Anichron.Worker.Settings; |
| | | 4 | | using Microsoft.Extensions.Options; |
| | | 5 | | |
| | | 6 | | namespace Anichron.Worker.Crawling; |
| | | 7 | | |
| | 5 | 8 | | internal sealed partial class Worker( |
| | 5 | 9 | | IServiceScopeFactory scopeFactory, |
| | 5 | 10 | | WorkerState workerState, |
| | 5 | 11 | | IFileIngestionPipeline ingestionPipeline, |
| | 5 | 12 | | IOptions<WorkerSettings> options, |
| | 5 | 13 | | ILogger<Worker> logger) : BackgroundService |
| | | 14 | | { |
| | 5 | 15 | | private readonly WorkerSettings settings = options.Value; |
| | | 16 | | |
| | | 17 | | protected override async Task ExecuteAsync(CancellationToken stoppingToken) |
| | 0 | 18 | | { |
| | 0 | 19 | | var mode = workerState.ResolvedUserId is { } userId |
| | 0 | 20 | | ? $"dedicated (userId={userId})" |
| | 0 | 21 | | : "all-user"; |
| | 0 | 22 | | Log.Starting(logger, mode); |
| | | 23 | | |
| | 0 | 24 | | while (!stoppingToken.IsCancellationRequested) |
| | 0 | 25 | | { |
| | 0 | 26 | | await CrawlAllAsync(stoppingToken); |
| | 0 | 27 | | await Task.Delay(TimeSpan.FromHours(settings.CrawlIntervalHours), stoppingToken); |
| | 0 | 28 | | } |
| | 0 | 29 | | } |
| | | 30 | | |
| | | 31 | | internal async Task CrawlAllAsync(CancellationToken ct) |
| | 5 | 32 | | { |
| | 5 | 33 | | using var scope = scopeFactory.CreateScope(); |
| | 5 | 34 | | var repository = scope.ServiceProvider.GetRequiredService<IUserStorageConfigRepository>(); |
| | | 35 | | |
| | 5 | 36 | | var configs = workerState.ResolvedUserId is { } userId |
| | 5 | 37 | | ? await repository.GetActiveByUserIdAsync(userId, ct) |
| | 5 | 38 | | : await repository.GetAllActiveAsync(ct); |
| | | 39 | | |
| | 21 | 40 | | foreach (var config in configs) |
| | 3 | 41 | | await CrawlAsync(config, ct); |
| | 5 | 42 | | } |
| | | 43 | | |
| | | 44 | | private async Task CrawlAsync(UserStorageConfig config, CancellationToken ct) |
| | 3 | 45 | | { |
| | 3 | 46 | | Log.CrawlingPath(logger, config.RootPath, config.UserId); |
| | 3 | 47 | | await ingestionPipeline.RunAsync(config, ct); |
| | 3 | 48 | | } |
| | | 49 | | |
| | | 50 | | private static partial class Log |
| | | 51 | | { |
| | | 52 | | [LoggerMessage(Level = LogLevel.Information, Message = "Worker starting in {Mode} mode.")] |
| | | 53 | | public static partial void Starting(ILogger logger, string mode); |
| | | 54 | | |
| | | 55 | | [LoggerMessage(Level = LogLevel.Information, Message = "Crawling {RootPath} (userId={UserId}).")] |
| | | 56 | | public static partial void CrawlingPath(ILogger logger, string rootPath, Guid userId); |
| | | 57 | | } |
| | | 58 | | } |