< Summary

Line coverage
90%
Covered lines: 58
Uncovered lines: 6
Coverable lines: 64
Total lines: 1462
Line coverage: 90.6%
Branch coverage
100%
Covered branches: 16
Total branches: 16
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%210%
File 2: .ctor(...)100%11100%
File 2: StartAsync()100%1616100%
File 2: StopAsync(...)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/Startup/WorkerInitializer.cs

#LineLine coverage
 1using Anichron.Core;
 2using Anichron.Core.Data;
 3using Anichron.Core.Data.Repository;
 4using Anichron.Core.Domain;
 5using Anichron.Worker.Settings;
 6using Anichron.Worker.Work;
 7using Microsoft.Extensions.Options;
 8
 9namespace Anichron.Worker.Startup;
 10
 1811public sealed partial class WorkerInitializer(
 1812    IOptions<WorkerSettings> options,
 1813    IServiceScopeFactory scopeFactory,
 1814    WorkerState workerState,
 1815    ILogger<WorkerInitializer> logger) : IHostedService
 16{
 1817    private readonly WorkerSettings settings = options.Value;
 18
 19    public async Task StartAsync(CancellationToken cancellationToken)
 1620    {
 1621        if (string.IsNullOrWhiteSpace(settings.User))
 222        {
 223            Log.AllUserMode(logger);
 224            return;
 25        }
 26
 1427        await using var scope = scopeFactory.CreateAsyncScope();
 1428        var userRepository = scope.ServiceProvider.GetRequiredService<IUserRepository>();
 1429        var storageConfigRepository = scope.ServiceProvider.GetRequiredService<IUserStorageConfigRepository>();
 1430        var unitOfWork = scope.ServiceProvider.GetRequiredService<IUnitOfWork>();
 31
 1432        var user = await userRepository.FindByCredentialAsync(settings.User, cancellationToken)
 1433            ?? throw new InvalidOperationException(
 1434                $"WORKER__USER '{settings.User}' not found in database.");
 35
 1336        if (string.IsNullOrWhiteSpace(settings.RootPath))
 237        {
 238            throw new InvalidOperationException(
 239                $"WORKER__ROOTPATH is required when WORKER__USER is set, e.g. {StoragePath.OriginalsRoot}/<user>.");
 40        }
 41
 1142        var rootPath = StoragePath.Canonicalize(settings.RootPath)
 1143            ?? throw new InvalidOperationException(
 1144                $"WORKER__ROOTPATH '{settings.RootPath}' must be an absolute path.");
 1045        if (!StoragePath.IsInsideOriginals(rootPath))
 246        {
 247            throw new InvalidOperationException(
 248                $"WORKER__ROOTPATH '{rootPath}' must be a folder inside {StoragePath.OriginalsRoot}: " +
 249                "use the container path, not the host path.");
 50        }
 51
 852        var existing = await storageConfigRepository.FindByRootPathAsync(rootPath, cancellationToken);
 853        if (existing is not null)
 354        {
 355            if (existing.UserId != user.Id)
 156            {
 157                throw new InvalidOperationException(
 158                    $"Path '{rootPath}' is already assigned to another user.");
 59            }
 60
 261            Log.StorageConfigExists(logger, user.Username, rootPath);
 262            workerState.ResolvedUserId = user.Id;
 263            return;
 64        }
 65
 566        var overlapping = StoragePath.FindOverlap(
 567            await storageConfigRepository.GetAllRootPathsAsync(cancellationToken), rootPath);
 568        if (overlapping is not null)
 269        {
 270            throw new InvalidOperationException(
 271                $"Path '{rootPath}' overlaps the existing storage config at '{overlapping}'.");
 72        }
 73
 374        storageConfigRepository.Add(new UserStorageConfig
 375        {
 376            Id = Guid.NewGuid(),
 377            UserId = user.Id,
 378            RootPath = rootPath,
 379            IsActive = true,
 380        });
 381        await unitOfWork.SaveChangesAsync(cancellationToken);
 82
 383        Log.StorageConfigCreated(logger, user.Username, rootPath);
 384        workerState.ResolvedUserId = user.Id;
 785    }
 86
 187    public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
 88
 89    private static partial class Log
 90    {
 91        [LoggerMessage(Level = LogLevel.Information, Message = "Starting in all-user mode.")]
 92        public static partial void AllUserMode(ILogger logger);
 93
 94        [LoggerMessage(Level = LogLevel.Information, Message = "Storage config already exists for '{Username}' at '{Root
 95        public static partial void StorageConfigExists(ILogger logger, string username, string root);
 96
 97        [LoggerMessage(Level = LogLevel.Information, Message = "Created storage config for '{Username}' at '{Root}'.")]
 98        public static partial void StorageConfigCreated(ILogger logger, string username, string root);
 99    }
 100}