| | | 1 | | using Anichron.Core.Data; |
| | | 2 | | using Anichron.Core.Data.Repository; |
| | | 3 | | using Anichron.Core.Domain; |
| | | 4 | | using Anichron.Worker.Ingestion.Classification; |
| | | 5 | | using Anichron.Worker.Ingestion.Pipeline; |
| | | 6 | | using Microsoft.EntityFrameworkCore; |
| | | 7 | | |
| | | 8 | | namespace Anichron.Worker.Ingestion; |
| | | 9 | | |
| | 147 | 10 | | internal sealed class IngestionSaver(IUnitOfWork unitOfWork, IMediaAssetRepository repository) |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Saves everything the item staged, in its one save: when the classification has |
| | | 14 | | /// <see cref="IngestionClassification.RetiredFirst"/> rows, they are soft-deleted first, in one transaction. After |
| | | 15 | | /// commit, and on a lost race, the proxies of every staged video are released. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <param name="context">The classified item whose staged changes and counts the save commits.</param> |
| | | 18 | | /// <param name="processed">The row with newly processed content; null when the item only reconciled stored rows.</p |
| | | 19 | | /// <param name="ct">Cancels the save.</param> |
| | | 20 | | /// <returns> |
| | | 21 | | /// False when another consumer took the content first, for a processed row whose content another row committed or |
| | | 22 | | /// for an item that retires rows first; nothing is saved then. |
| | | 23 | | /// </returns> |
| | | 24 | | public async Task<bool> TrySaveItemAsync(IngestionContext context, MediaAsset? processed, CancellationToken ct) |
| | 126 | 25 | | { |
| | 126 | 26 | | var retiredFirst = context.Classification!.RetiredFirst; |
| | | 27 | | try |
| | 126 | 28 | | { |
| | 126 | 29 | | await SaveAsync(context, retiredFirst, ct); |
| | 95 | 30 | | return true; |
| | | 31 | | } |
| | 31 | 32 | | catch (DbUpdateException ex) when (LostIngestionRace.Matches(ex)) |
| | 22 | 33 | | { |
| | 22 | 34 | | if (processed is not null) |
| | 11 | 35 | | { |
| | 11 | 36 | | if (await repository.FindByHashAsync(processed.ContentHash, processed.StorageConfigId, ct) is null) |
| | 4 | 37 | | throw; |
| | 7 | 38 | | return false; |
| | | 39 | | } |
| | | 40 | | |
| | 11 | 41 | | if (retiredFirst.Count == 0) |
| | 7 | 42 | | throw; |
| | 4 | 43 | | return false; |
| | | 44 | | } |
| | 106 | 45 | | } |
| | | 46 | | |
| | | 47 | | private async Task SaveAsync(IngestionContext context, IReadOnlyList<MediaAsset> retiredFirst, CancellationToken ct) |
| | 126 | 48 | | { |
| | | 49 | | try |
| | 126 | 50 | | { |
| | 126 | 51 | | if (retiredFirst.Count == 0) |
| | 109 | 52 | | await unitOfWork.SaveChangesAsync(ct); |
| | | 53 | | else |
| | 17 | 54 | | await SaveRetiringFirstAsync(retiredFirst, ct); |
| | 95 | 55 | | } |
| | 31 | 56 | | catch (DbUpdateException ex) when (LostIngestionRace.Matches(ex)) |
| | 22 | 57 | | { |
| | 22 | 58 | | context.ReleaseStagedMovProxies(); |
| | 22 | 59 | | throw; |
| | | 60 | | } |
| | | 61 | | |
| | 95 | 62 | | context.CommitCounts(); |
| | 95 | 63 | | context.ReleaseStagedMovProxies(); |
| | 95 | 64 | | } |
| | | 65 | | |
| | | 66 | | private Task SaveRetiringFirstAsync(IReadOnlyList<MediaAsset> retired, CancellationToken ct) |
| | 17 | 67 | | => unitOfWork.ExecuteInTransactionAsync( |
| | 17 | 68 | | async () => |
| | 17 | 69 | | { |
| | 17 | 70 | | await repository.SoftDeleteAsync([.. retired.Select(row => row.Id)], ct); |
| | 17 | 71 | | await unitOfWork.SaveChangesAsync(ct); |
| | 17 | 72 | | }, |
| | 17 | 73 | | ct); |
| | | 74 | | } |