< Summary

Line coverage
100%
Covered lines: 73
Uncovered lines: 0
Coverable lines: 73
Total lines: 1382
Line coverage: 100%
Branch coverage
95%
Covered branches: 21
Total branches: 22
Branch coverage: 95.4%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
File 1: .cctor()100%11100%
File 2: .ctor(...)100%11100%
File 2: RunAsync()87.5%88100%
File 2: DeleteAsync()100%88100%
File 2: RemoveContentDirectoryIfEmpty(...)100%66100%

File(s)

/home/runner/work/anichron/anichron/src/.artifacts/obj/Anichron.Worker/debug/Microsoft.Extensions.Logging.Generators/Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator/LoggerMessage.g.cs

File '/home/runner/work/anichron/anichron/src/.artifacts/obj/Anichron.Worker/debug/Microsoft.Extensions.Logging.Generators/Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator/LoggerMessage.g.cs' does not exist (any more).

/home/runner/work/anichron/anichron/src/Anichron.Worker/ProxySweeps/ProxyDeletion.cs

#LineLine coverage
 1using Anichron.Core;
 2using Anichron.Core.Data;
 3using Anichron.Core.Domain;
 4using Anichron.Core.Work;
 5using Anichron.Worker.Ingestion.Proxy;
 6using NodaTime;
 7using System.IO.Abstractions;
 8
 9namespace Anichron.Worker.ProxySweeps;
 10
 11public 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
 2326internal sealed partial class ProxyDeletionHandler(
 2327    IProxyOrphanStore store,
 2328    IUnitOfWork unitOfWork,
 2329    IProxyDirectoryStrategy directories,
 2330    IFileSystem fileSystem,
 2331    IClock clock,
 2332    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)
 2237    {
 2238        var result = ProxyDeletionResult.None;
 2239        var done = 0;
 4440        while (await store.GetPendingAsync(payload, BatchSize, ct) is { Count: > 0 } batch)
 2241        {
 11842            foreach (var orphan in batch)
 2643            {
 2644                var outcome = await DeleteAsync(orphan, ct);
 2645                orphan.DeletionOutcome = outcome;
 2646                orphan.ProcessedAt = clock.GetCurrentInstant();
 2647                await unitOfWork.SaveChangesAsync(ct);
 2648                result = result.Counting(outcome);
 2649                run.Progress.Report(++done, note: "Deleting orphaned proxy files");
 2650            }
 51
 2252            store.ForgetLoaded();
 2253        }
 54
 2255        Log.Finished(logger, payload.SweepId, result.Deleted, result.SkippedReferenced, result.SkippedChanged, result.Mi
 2256        return result;
 2257    }
 58
 59    private async Task<ProxyDeletionOutcome> DeleteAsync(ProxyOrphan orphan, CancellationToken ct)
 2660    {
 2661        if (await store.IsReferencedAsync(orphan.ProxyPath, ct))
 162            return ProxyDeletionOutcome.SkippedReferenced;
 63
 2564        if (StoragePath.ResolveProxy(orphan.ProxyPath) is not { } file)
 165        {
 166            Log.OutsideRoot(logger, orphan.ProxyPath);
 167            return ProxyDeletionOutcome.Failed;
 68        }
 69
 2470        if (!fileSystem.File.Exists(file))
 771            return ProxyDeletionOutcome.Missing;
 1772        if (ProxyFileStamp.Of(fileSystem.FileInfo.New(file)) != new ProxyFileStamp(orphan.SizeBytes, orphan.LastWriteTim
 273            return ProxyDeletionOutcome.SkippedChanged;
 74
 75        try
 1576        {
 1577            fileSystem.File.Delete(file);
 1478        }
 179        catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
 180        {
 181            Log.DeleteFailed(logger, orphan.ProxyPath, ex);
 182            return ProxyDeletionOutcome.Failed;
 83        }
 84
 1485        RemoveContentDirectoryIfEmpty(orphan.ProxyPath);
 1486        return ProxyDeletionOutcome.Deleted;
 2687    }
 88
 89    private void RemoveContentDirectoryIfEmpty(string proxyPath)
 1490    {
 1491        if (directories.ContentDirectoryOf(proxyPath) is not { } content)
 492            return;
 1093        if (StoragePath.ResolveProxy(content) is not { } directory)
 194        {
 195            Log.DirectoryOutsideRoot(logger, content);
 196            return;
 97        }
 98
 99        try
 9100        {
 9101            if (!fileSystem.Directory.EnumerateFileSystemEntries(directory).Any())
 6102                fileSystem.Directory.Delete(directory);
 8103        }
 1104        catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
 1105        {
 1106            Log.DirectoryKept(logger, directory, ex);
 1107        }
 14108    }
 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}