| | | 1 | | using Anichron.Core.Data.Repository; |
| | | 2 | | using Anichron.Core.Domain; |
| | | 3 | | using Anichron.Core.Work; |
| | | 4 | | using Anichron.Worker.Ingestion; |
| | | 5 | | using Anichron.Worker.Work; |
| | | 6 | | |
| | | 7 | | namespace Anichron.Worker.Crawling; |
| | | 8 | | |
| | 15 | 9 | | internal sealed partial class CrawlHandler( |
| | 15 | 10 | | ActiveStorageConfigs activeConfigs, |
| | 15 | 11 | | IUserStorageConfigRepository configs, |
| | 15 | 12 | | IFileIngestionPipeline pipeline, |
| | 15 | 13 | | IMissingFileSweep sweep, |
| | 15 | 14 | | ISharedContentReporter reporter, |
| | 15 | 15 | | IWorkQueue queue, |
| | 15 | 16 | | ILogger<CrawlHandler> logger) : IWorkHandler<CrawlPayload> |
| | | 17 | | { |
| | | 18 | | public async Task<object?> RunAsync(CrawlPayload payload, WorkRun run, CancellationToken ct) |
| | 15 | 19 | | { |
| | 15 | 20 | | if (await activeConfigs.FindAsync(CrawlPayload.Kind, payload.StorageConfigId, ct) is not { } config) |
| | 2 | 21 | | return StorageConfigSkipped.MissingOrInactive; |
| | | 22 | | |
| | 13 | 23 | | Log.Crawling(logger, config.RootPath, config.UserId); |
| | 13 | 24 | | var crawl = await pipeline.RunAsync(config, run.Progress, ct); |
| | 10 | 25 | | var reconciliation = await ReconcileAsync(config, crawl, ct); |
| | 10 | 26 | | var result = crawl.Result with |
| | 10 | 27 | | { |
| | 10 | 28 | | Duplicates = await ReportDuplicatesAsync(config, crawl.DuplicateCopies, ct), |
| | 10 | 29 | | Reconciliation = reconciliation, |
| | 10 | 30 | | }; |
| | 9 | 31 | | await queue.EnqueueAsync(new BurstDetectionPayload(config.Id), null, ct); |
| | 9 | 32 | | if (result.Ingested > 0) |
| | 2 | 33 | | await queue.EnqueueAsync(CompactPreviewPayload.For(config.Id), null, ct); |
| | 9 | 34 | | return result; |
| | 11 | 35 | | } |
| | | 36 | | |
| | | 37 | | private async Task<ReconciliationCounts> ReconcileAsync(UserStorageConfig config, CrawlRun run, CancellationToken ct |
| | 10 | 38 | | { |
| | 10 | 39 | | var missing = await sweep.SweepAsync(config, run, ct); |
| | 10 | 40 | | var counts = new ReconciliationCounts(run.SoftDeleted + missing.SoftDeleted, run.AutoRestored, missing.SkippedBe |
| | 10 | 41 | | if (counts.SkippedBecause is { } reason) |
| | 1 | 42 | | Log.ReconciledWithoutSweep(logger, config.Id, counts.SoftDeleted, counts.AutoRestored, reason); |
| | | 43 | | else |
| | 9 | 44 | | Log.Reconciled(logger, config.Id, counts.SoftDeleted, counts.AutoRestored); |
| | 10 | 45 | | return counts; |
| | 10 | 46 | | } |
| | | 47 | | |
| | | 48 | | private async Task<DuplicateCounts?> ReportDuplicatesAsync( |
| | | 49 | | UserStorageConfig config, int copiesWithin, CancellationToken ct) |
| | 10 | 50 | | { |
| | | 51 | | try |
| | 10 | 52 | | { |
| | 10 | 53 | | var usersConfigs = await configs.GetActiveByUserIdAsync(config.UserId, ct); |
| | 10 | 54 | | return await reporter.ReportAsync(config, usersConfigs, copiesWithin, ct); |
| | | 55 | | } |
| | 2 | 56 | | catch (Exception ex) when (!IngestionCancellation.IsRequested(ex, ct)) |
| | 1 | 57 | | { |
| | 1 | 58 | | Log.DuplicateReportFailed(logger, config.Id, ex); |
| | 1 | 59 | | return null; |
| | | 60 | | } |
| | 9 | 61 | | } |
| | | 62 | | |
| | | 63 | | private static partial class Log |
| | | 64 | | { |
| | | 65 | | [LoggerMessage(Level = LogLevel.Information, Message = "Crawling {RootPath} (userId={UserId}).")] |
| | | 66 | | public static partial void Crawling(ILogger logger, string rootPath, Guid userId); |
| | | 67 | | |
| | | 68 | | [LoggerMessage( |
| | | 69 | | Level = LogLevel.Information, |
| | | 70 | | Message = "Reconciled storage config {Id}: {SoftDeleted} soft-deleted, {AutoRestored} auto-restored.")] |
| | | 71 | | public static partial void Reconciled(ILogger logger, Guid id, int softDeleted, int autoRestored); |
| | | 72 | | |
| | | 73 | | [LoggerMessage( |
| | | 74 | | Level = LogLevel.Information, |
| | | 75 | | Message = "Reconciled storage config {Id}: {SoftDeleted} soft-deleted, {AutoRestored} auto-restored; " |
| | | 76 | | + "missing files were not looked for ({Reason}).")] |
| | | 77 | | public static partial void ReconciledWithoutSweep(ILogger logger, Guid id, int softDeleted, int autoRestored, Mi |
| | | 78 | | |
| | | 79 | | [LoggerMessage(Level = LogLevel.Error, Message = "Reporting duplicate content for storage config {Id} failed.")] |
| | | 80 | | public static partial void DuplicateReportFailed(ILogger logger, Guid id, Exception ex); |
| | | 81 | | } |
| | | 82 | | } |