| | | 1 | | using Anichron.Core; |
| | | 2 | | using Anichron.Core.Data; |
| | | 3 | | using Anichron.Core.Domain; |
| | | 4 | | using Anichron.Core.Work; |
| | | 5 | | using Anichron.Worker.Ingestion.Proxy; |
| | | 6 | | using NodaTime; |
| | | 7 | | using System.IO.Abstractions; |
| | | 8 | | |
| | | 9 | | namespace Anichron.Worker.ProxySweeps; |
| | | 10 | | |
| | | 11 | | public sealed record ProxyDeletionResult(int Deleted, int SkippedReferenced, int SkippedChanged, int Missing, int Failed |
| | | 12 | | { |
| | | 13 | | public static ProxyDeletionResult None { get; } = new(0, 0, 0, 0, 0); |
| | | 14 | | |
| | | 15 | | public ProxyDeletionResult Counting(ProxyDeletionOutcome outcome) => outcome switch |
| | | 16 | | { |
| | | 17 | | ProxyDeletionOutcome.Deleted => this with { Deleted = Deleted + 1 }, |
| | | 18 | | ProxyDeletionOutcome.SkippedReferenced => this with { SkippedReferenced = SkippedReferenced + 1 }, |
| | | 19 | | ProxyDeletionOutcome.SkippedChanged => this with { SkippedChanged = SkippedChanged + 1 }, |
| | | 20 | | ProxyDeletionOutcome.Missing => this with { Missing = Missing + 1 }, |
| | | 21 | | ProxyDeletionOutcome.Failed => this with { Failed = Failed + 1 }, |
| | | 22 | | _ => throw new ArgumentOutOfRangeException(nameof(outcome), outcome, "Not a deletion outcome."), |
| | | 23 | | }; |
| | | 24 | | } |
| | | 25 | | |
| | 23 | 26 | | internal sealed partial class ProxyDeletionHandler( |
| | 23 | 27 | | IProxyOrphanStore store, |
| | 23 | 28 | | IUnitOfWork unitOfWork, |
| | 23 | 29 | | IProxyDirectoryStrategy directories, |
| | 23 | 30 | | IFileSystem fileSystem, |
| | 23 | 31 | | IClock clock, |
| | 23 | 32 | | ILogger<ProxyDeletionHandler> logger) : IWorkHandler<ProxyDeletionPayload> |
| | | 33 | | { |
| | | 34 | | internal const int BatchSize = 500; |
| | | 35 | | |
| | | 36 | | public async Task<object?> RunAsync(ProxyDeletionPayload payload, WorkRun run, CancellationToken ct) |
| | 22 | 37 | | { |
| | 22 | 38 | | var result = ProxyDeletionResult.None; |
| | 22 | 39 | | var done = 0; |
| | 44 | 40 | | while (await store.GetPendingAsync(payload, BatchSize, ct) is { Count: > 0 } batch) |
| | 22 | 41 | | { |
| | 118 | 42 | | foreach (var orphan in batch) |
| | 26 | 43 | | { |
| | 26 | 44 | | var outcome = await DeleteAsync(orphan, ct); |
| | 26 | 45 | | orphan.DeletionOutcome = outcome; |
| | 26 | 46 | | orphan.ProcessedAt = clock.GetCurrentInstant(); |
| | 26 | 47 | | await unitOfWork.SaveChangesAsync(ct); |
| | 26 | 48 | | result = result.Counting(outcome); |
| | 26 | 49 | | run.Progress.Report(++done, note: "Deleting orphaned proxy files"); |
| | 26 | 50 | | } |
| | | 51 | | |
| | 22 | 52 | | store.ForgetLoaded(); |
| | 22 | 53 | | } |
| | | 54 | | |
| | 22 | 55 | | Log.Finished(logger, payload.SweepId, result.Deleted, result.SkippedReferenced, result.SkippedChanged, result.Mi |
| | 22 | 56 | | return result; |
| | 22 | 57 | | } |
| | | 58 | | |
| | | 59 | | private async Task<ProxyDeletionOutcome> DeleteAsync(ProxyOrphan orphan, CancellationToken ct) |
| | 26 | 60 | | { |
| | 26 | 61 | | if (await store.IsReferencedAsync(orphan.ProxyPath, ct)) |
| | 1 | 62 | | return ProxyDeletionOutcome.SkippedReferenced; |
| | | 63 | | |
| | 25 | 64 | | if (StoragePath.ResolveProxy(orphan.ProxyPath) is not { } file) |
| | 1 | 65 | | { |
| | 1 | 66 | | Log.OutsideRoot(logger, orphan.ProxyPath); |
| | 1 | 67 | | return ProxyDeletionOutcome.Failed; |
| | | 68 | | } |
| | | 69 | | |
| | 24 | 70 | | if (!fileSystem.File.Exists(file)) |
| | 7 | 71 | | return ProxyDeletionOutcome.Missing; |
| | 17 | 72 | | if (ProxyFileStamp.Of(fileSystem.FileInfo.New(file)) != new ProxyFileStamp(orphan.SizeBytes, orphan.LastWriteTim |
| | 2 | 73 | | return ProxyDeletionOutcome.SkippedChanged; |
| | | 74 | | |
| | | 75 | | try |
| | 15 | 76 | | { |
| | 15 | 77 | | fileSystem.File.Delete(file); |
| | 14 | 78 | | } |
| | 1 | 79 | | catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) |
| | 1 | 80 | | { |
| | 1 | 81 | | Log.DeleteFailed(logger, orphan.ProxyPath, ex); |
| | 1 | 82 | | return ProxyDeletionOutcome.Failed; |
| | | 83 | | } |
| | | 84 | | |
| | 14 | 85 | | RemoveContentDirectoryIfEmpty(orphan.ProxyPath); |
| | 14 | 86 | | return ProxyDeletionOutcome.Deleted; |
| | 26 | 87 | | } |
| | | 88 | | |
| | | 89 | | private void RemoveContentDirectoryIfEmpty(string proxyPath) |
| | 14 | 90 | | { |
| | 14 | 91 | | if (directories.ContentDirectoryOf(proxyPath) is not { } content) |
| | 4 | 92 | | return; |
| | 10 | 93 | | if (StoragePath.ResolveProxy(content) is not { } directory) |
| | 1 | 94 | | { |
| | 1 | 95 | | Log.DirectoryOutsideRoot(logger, content); |
| | 1 | 96 | | return; |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | try |
| | 9 | 100 | | { |
| | 9 | 101 | | if (!fileSystem.Directory.EnumerateFileSystemEntries(directory).Any()) |
| | 6 | 102 | | fileSystem.Directory.Delete(directory); |
| | 8 | 103 | | } |
| | 1 | 104 | | catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) |
| | 1 | 105 | | { |
| | 1 | 106 | | Log.DirectoryKept(logger, directory, ex); |
| | 1 | 107 | | } |
| | 14 | 108 | | } |
| | | 109 | | |
| | | 110 | | private static partial class Log |
| | | 111 | | { |
| | | 112 | | [LoggerMessage( |
| | | 113 | | Level = LogLevel.Information, |
| | | 114 | | Message = "Deleted orphaned proxy files of sweep {SweepId}: {Deleted} deleted, {SkippedReferenced} reference |
| | | 115 | | + "{SkippedChanged} changed, {Missing} missing, {Failed} failed.")] |
| | | 116 | | public static partial void Finished( |
| | | 117 | | ILogger logger, Guid sweepId, int deleted, int skippedReferenced, int skippedChanged, int missing, int faile |
| | | 118 | | |
| | | 119 | | [LoggerMessage(Level = LogLevel.Warning, Message = "Could not delete orphaned proxy file {ProxyPath}.")] |
| | | 120 | | public static partial void DeleteFailed(ILogger logger, string proxyPath, Exception ex); |
| | | 121 | | |
| | | 122 | | [LoggerMessage(Level = LogLevel.Warning, Message = "Did not delete orphaned proxy file {ProxyPath}: it resolves |
| | | 123 | | public static partial void OutsideRoot(ILogger logger, string proxyPath); |
| | | 124 | | |
| | | 125 | | [LoggerMessage(Level = LogLevel.Warning, Message = "Kept proxy directory {Directory}: it resolves outside the pr |
| | | 126 | | public static partial void DirectoryOutsideRoot(ILogger logger, string directory); |
| | | 127 | | |
| | | 128 | | [LoggerMessage(Level = LogLevel.Debug, Message = "Kept proxy directory {Directory}: it could not be removed.")] |
| | | 129 | | public static partial void DirectoryKept(ILogger logger, string directory, Exception ex); |
| | | 130 | | } |
| | | 131 | | } |