| | | 1 | | using Anichron.Worker.Ingestion.Classification; |
| | | 2 | | using Anichron.Worker.Ingestion.Pipeline; |
| | | 3 | | |
| | | 4 | | namespace Anichron.Worker.Ingestion.Middlewares; |
| | | 5 | | |
| | 57 | 6 | | internal sealed partial class ClassificationMiddleware( |
| | 57 | 7 | | IngestionClassifier classifier, |
| | 57 | 8 | | LivePhotoPairer pairer, |
| | 57 | 9 | | KnownFileReconciler reconciler, |
| | 57 | 10 | | IngestionSaver saver, |
| | 57 | 11 | | ILogger<ClassificationMiddleware> logger) : IIngestionMiddleware |
| | | 12 | | { |
| | 1 | 13 | | public int Order => IngestionOrder.Classification; |
| | 3 | 14 | | public bool CanInvoke(IngestionContext context) => true; |
| | | 15 | | |
| | | 16 | | public async Task InvokeAsync(IngestionContext context, IngestionDelegate next, CancellationToken ct) |
| | 55 | 17 | | { |
| | 55 | 18 | | var classification = await classifier.ClassifyAsync(context, ct); |
| | 55 | 19 | | context.Classification = classification; |
| | 55 | 20 | | context.AssetId = classification.Primary.Stored?.Id ?? context.AttemptId; |
| | 55 | 21 | | if (classification.NeedsProcessing) |
| | 7 | 22 | | { |
| | 7 | 23 | | await ReconcileSecondaryAsync(context, classification, ct); |
| | 7 | 24 | | await next(context, ct); |
| | 7 | 25 | | return; |
| | | 26 | | } |
| | | 27 | | |
| | 48 | 28 | | await pairer.PairAsync(context, classification, ct); |
| | | 29 | | |
| | 48 | 30 | | var changed = await reconciler.ReconcileAsync(context, classification.Primary, context.Item.RelativePath, ct); |
| | 48 | 31 | | if (await ReconcileSecondaryAsync(context, classification, ct)) |
| | 22 | 32 | | changed = true; |
| | | 33 | | |
| | 48 | 34 | | if (changed && !await saver.TrySaveItemAsync(context, null, ct)) |
| | 2 | 35 | | Log.ContentTakenConcurrently(logger, context.Item.RelativePath); |
| | 48 | 36 | | } |
| | | 37 | | |
| | | 38 | | private async Task<bool> ReconcileSecondaryAsync( |
| | | 39 | | IngestionContext context, IngestionClassification classification, CancellationToken ct) |
| | 55 | 40 | | => classification.Secondary is { } secondary |
| | 55 | 41 | | && await reconciler.ReconcileSecondaryAsync(context, secondary, classification.Primary, context.Item.Seconda |
| | | 42 | | |
| | | 43 | | private static partial class Log |
| | | 44 | | { |
| | | 45 | | [LoggerMessage( |
| | | 46 | | Level = LogLevel.Debug, |
| | | 47 | | Message = "Another consumer took the content now at {RelativePath} first, skipping; the next crawl reconcile |
| | | 48 | | public static partial void ContentTakenConcurrently(ILogger logger, string relativePath); |
| | | 49 | | } |
| | | 50 | | } |