| | | 1 | | using Anichron.Worker.Ingestion.Classification; |
| | | 2 | | using Anichron.Worker.Ingestion.Pipeline; |
| | | 3 | | |
| | | 4 | | namespace Anichron.Worker.Ingestion.Middlewares; |
| | | 5 | | |
| | 67 | 6 | | internal sealed partial class PersistenceMiddleware( |
| | 67 | 7 | | IEnumerable<IOutcomeWriter> writers, |
| | 67 | 8 | | IngestionSaver saver, |
| | 67 | 9 | | ILogger<PersistenceMiddleware> logger) : IIngestionMiddleware |
| | | 10 | | { |
| | 0 | 11 | | public int Order => IngestionOrder.Persistence; |
| | 3 | 12 | | public bool CanInvoke(IngestionContext context) => true; |
| | | 13 | | |
| | | 14 | | public async Task InvokeAsync(IngestionContext context, IngestionDelegate next, CancellationToken ct) |
| | 66 | 15 | | { |
| | 66 | 16 | | var outcome = context.Classification!.Primary.Outcome; |
| | 66 | 17 | | var writer = writers.Single(candidate => candidate.Outcomes.Contains(outcome)); |
| | 65 | 18 | | var asset = await writer.WriteAsync(context, ct); |
| | 65 | 19 | | if (!await saver.TrySaveItemAsync(context, asset, ct)) |
| | 4 | 20 | | { |
| | 4 | 21 | | Log.AlreadyPersisted(logger, context.Item.RelativePath); |
| | 4 | 22 | | return; |
| | | 23 | | } |
| | | 24 | | |
| | 55 | 25 | | context.Asset = asset; |
| | 55 | 26 | | Log.Persisted(logger, context.Item.RelativePath, asset.Id); |
| | 55 | 27 | | await next(context, ct); |
| | 59 | 28 | | } |
| | | 29 | | |
| | | 30 | | private static partial class Log |
| | | 31 | | { |
| | | 32 | | [LoggerMessage(Level = LogLevel.Information, Message = "Persisted {RelativePath} as asset {AssetId}")] |
| | | 33 | | public static partial void Persisted(ILogger logger, string relativePath, Guid assetId); |
| | | 34 | | |
| | | 35 | | [LoggerMessage(Level = LogLevel.Debug, Message = "Content of {RelativePath} was persisted concurrently by anothe |
| | | 36 | | public static partial void AlreadyPersisted(ILogger logger, string relativePath); |
| | | 37 | | } |
| | | 38 | | } |