< Summary

Line coverage
87%
Covered lines: 41
Uncovered lines: 6
Coverable lines: 47
Total lines: 476
Line coverage: 87.2%
Branch coverage
100%
Covered branches: 8
Total branches: 8
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%88100%
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.Data;
 2using Anichron.Core.Data.Repository;
 3using Anichron.Core.Domain;
 4using Anichron.Worker.Crawling;
 5using Anichron.Worker.Settings;
 6using Microsoft.Extensions.Options;
 7
 8namespace Anichron.Worker.Startup;
 9
 810public sealed partial class WorkerInitializer(
 811    IOptions<WorkerSettings> options,
 812    IServiceScopeFactory scopeFactory,
 813    WorkerState workerState,
 814    ILogger<WorkerInitializer> logger) : IHostedService
 15{
 816    private readonly WorkerSettings settings = options.Value;
 17
 18    public async Task StartAsync(CancellationToken cancellationToken)
 719    {
 720        if (string.IsNullOrWhiteSpace(settings.User))
 221        {
 222            Log.AllUserMode(logger);
 223            return;
 24        }
 25
 526        await using var scope = scopeFactory.CreateAsyncScope();
 527        var userRepository = scope.ServiceProvider.GetRequiredService<IUserRepository>();
 528        var storageConfigRepository = scope.ServiceProvider.GetRequiredService<IUserStorageConfigRepository>();
 529        var unitOfWork = scope.ServiceProvider.GetRequiredService<IUnitOfWork>();
 30
 531        var credential = settings.User.Trim().ToLowerInvariant();
 532        var user = await userRepository.FindByCredentialAsync(credential, cancellationToken)
 533            ?? throw new InvalidOperationException(
 534                $"WORKER__USER '{settings.User}' not found in database.");
 35
 436        var existing = await storageConfigRepository.FindByRootPathAsync(settings.RootPath, cancellationToken);
 437        if (existing is not null)
 338        {
 339            if (existing.UserId != user.Id)
 140            {
 141                throw new InvalidOperationException(
 142                    $"Path '{settings.RootPath}' is already assigned to another user.");
 43            }
 44
 245            Log.StorageConfigExists(logger, user.Username, settings.RootPath);
 246            workerState.ResolvedUserId = user.Id;
 247            return;
 48        }
 49
 150        storageConfigRepository.Add(new UserStorageConfig
 151        {
 152            Id = Guid.NewGuid(),
 153            UserId = user.Id,
 154            RootPath = settings.RootPath,
 155            IsActive = true,
 156        });
 157        await unitOfWork.SaveChangesAsync(cancellationToken);
 58
 159        Log.StorageConfigCreated(logger, user.Username, settings.RootPath);
 160        workerState.ResolvedUserId = user.Id;
 561    }
 62
 163    public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
 64
 65    private static partial class Log
 66    {
 67        [LoggerMessage(Level = LogLevel.Information, Message = "Starting in all-user mode.")]
 68        public static partial void AllUserMode(ILogger logger);
 69
 70        [LoggerMessage(Level = LogLevel.Information, Message = "Storage config already exists for '{Username}' at '{Root
 71        public static partial void StorageConfigExists(ILogger logger, string username, string root);
 72
 73        [LoggerMessage(Level = LogLevel.Information, Message = "Created storage config for '{Username}' at '{Root}'.")]
 74        public static partial void StorageConfigCreated(ILogger logger, string username, string root);
 75    }
 76}