< Summary

Line coverage
100%
Covered lines: 52
Uncovered lines: 0
Coverable lines: 52
Total lines: 1365
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
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()100%44100%
File 2: ScanRootAsync()100%66100%
File 2: ScanShardAsync()100%11100%
File 2: FilesUnder(...)100%11100%
File 2: Unreferenced(...)100%22100%
File 2: RelativePath(...)100%11100%

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/ProxySweep.cs

#LineLine coverage
 1using Anichron.Core;
 2using Anichron.Core.Work;
 3using Anichron.Worker.Ingestion.Proxy;
 4using NodaTime;
 5using System.IO.Abstractions;
 6
 7namespace Anichron.Worker.ProxySweeps;
 8
 9/// <summary>What a sweep reported.</summary>
 10/// <param name="Orphans">Files no proxy row references, last written more than the grace period before the scan.</param
 11/// <param name="Bytes">Their size in all.</param>
 12/// <param name="TemporaryFiles">Orphans left by an unfinished proxy write.</param>
 13/// <param name="ConfigsGone">Storage configs named by orphans' directories that no longer exist.</param>
 14public sealed record ProxySweepResult(int Orphans, long Bytes, int TemporaryFiles, int ConfigsGone);
 15
 1516internal sealed partial class ProxySweepHandler(
 1517    IProxyOrphanStore store,
 1518    IProxyDirectoryStrategy directories,
 1519    IFileSystem fileSystem,
 1520    IClock clock,
 1521    ILogger<ProxySweepHandler> logger) : IWorkHandler<ProxySweepPayload>
 22{
 23    internal const string ScanningNote = "Scanning proxy shards";
 24
 25    internal static Duration GracePeriod { get; } = Duration.FromHours(1);
 26
 27    public async Task<object?> RunAsync(ProxySweepPayload payload, WorkRun run, CancellationToken ct)
 1428    {
 1429        if (!fileSystem.Directory.Exists(StoragePath.ProxiesRoot))
 130            throw new DirectoryNotFoundException($"The proxy root {StoragePath.ProxiesRoot} is missing.");
 31
 1332        var writtenBefore = clock.GetCurrentInstant() - GracePeriod;
 1333        var atRoot = await ScanRootAsync(writtenBefore, ct);
 1334        await store.RecordAsync(run.ItemId, atRoot, ct);
 35
 1336        var shards = fileSystem.Directory.EnumerateDirectories(StoragePath.ProxiesRoot).Order(StringComparer.Ordinal).To
 4837        for (var i = 0; i < shards.Count; i++)
 1138        {
 1139            run.Progress.Report(i, shards.Count, ScanningNote);
 1140            await store.RecordAsync(run.ItemId, await ScanShardAsync(shards[i], writtenBefore, ct), ct);
 1141        }
 42
 1343        run.Progress.Report(shards.Count, shards.Count, ScanningNote);
 1344        var result = await store.SummariseAsync(run.ItemId, ct);
 1345        Log.Swept(logger, result.Orphans, result.Bytes, result.TemporaryFiles, result.ConfigsGone);
 1346        return result;
 1347    }
 48
 49    private async Task<List<FoundOrphan>> ScanRootAsync(Instant writtenBefore, CancellationToken ct)
 1350    {
 1351        var found = new List<FoundOrphan>();
 4552        foreach (var file in fileSystem.Directory.EnumerateFiles(StoragePath.ProxiesRoot))
 353        {
 354            var path = RelativePath(file);
 355            if (Unreferenced(file, path, writtenBefore) is { } orphan && !await store.IsReferencedAsync(path, ct))
 156                found.Add(orphan);
 357        }
 58
 1359        return found;
 1360    }
 61
 62    private async Task<List<FoundOrphan>> ScanShardAsync(string shard, Instant writtenBefore, CancellationToken ct)
 1163    {
 1164        var referenced = await store.ReferencedUnderAsync(fileSystem.Path.GetFileName(shard), ct);
 1165        return [.. FilesUnder(shard)
 1166            .Select(file => (File: file, Path: RelativePath(file)))
 1167            .Where(file => !referenced.Contains(file.Path))
 1168            .Select(file => Unreferenced(file.File, file.Path, writtenBefore))
 1169            .OfType<FoundOrphan>()];
 1170    }
 71
 72    private IEnumerable<string> FilesUnder(string directory)
 2373        => fileSystem.Directory.EnumerateFiles(directory)
 2374            .Concat(fileSystem.Directory.EnumerateDirectories(directory).SelectMany(FilesUnder));
 75
 76    private FoundOrphan? Unreferenced(string file, string path, Instant writtenBefore)
 1677    {
 1678        var stamp = ProxyFileStamp.Of(fileSystem.FileInfo.New(file));
 1679        return stamp.LastWriteTime < writtenBefore
 1680            ? new FoundOrphan(path, directories.StorageConfigOf(path), stamp.SizeBytes, stamp.LastWriteTime)
 1681            : null;
 1682    }
 83
 1784    private string RelativePath(string file) => fileSystem.Path.GetRelativePath(StoragePath.ProxiesRoot, file);
 85
 86    private static partial class Log
 87    {
 88        [LoggerMessage(
 89            Level = LogLevel.Information,
 90            Message = "Swept the proxy root: {Orphans} orphan(s), {Bytes} B, {TemporaryFiles} temporary file(s), {Config
 91        public static partial void Swept(ILogger logger, int orphans, long bytes, int temporaryFiles, int configsGone);
 92    }
 93}