| | | 1 | | using Anichron.Core; |
| | | 2 | | using Anichron.Core.Data; |
| | | 3 | | using Anichron.Core.Data.Repository; |
| | | 4 | | using Anichron.Core.Domain; |
| | | 5 | | using Anichron.Worker.Settings; |
| | | 6 | | using Anichron.Worker.Work; |
| | | 7 | | using Microsoft.Extensions.Options; |
| | | 8 | | |
| | | 9 | | namespace Anichron.Worker.Startup; |
| | | 10 | | |
| | 18 | 11 | | public sealed partial class WorkerInitializer( |
| | 18 | 12 | | IOptions<WorkerSettings> options, |
| | 18 | 13 | | IServiceScopeFactory scopeFactory, |
| | 18 | 14 | | WorkerState workerState, |
| | 18 | 15 | | ILogger<WorkerInitializer> logger) : IHostedService |
| | | 16 | | { |
| | 18 | 17 | | private readonly WorkerSettings settings = options.Value; |
| | | 18 | | |
| | | 19 | | public async Task StartAsync(CancellationToken cancellationToken) |
| | 16 | 20 | | { |
| | 16 | 21 | | if (string.IsNullOrWhiteSpace(settings.User)) |
| | 2 | 22 | | { |
| | 2 | 23 | | Log.AllUserMode(logger); |
| | 2 | 24 | | return; |
| | | 25 | | } |
| | | 26 | | |
| | 14 | 27 | | await using var scope = scopeFactory.CreateAsyncScope(); |
| | 14 | 28 | | var userRepository = scope.ServiceProvider.GetRequiredService<IUserRepository>(); |
| | 14 | 29 | | var storageConfigRepository = scope.ServiceProvider.GetRequiredService<IUserStorageConfigRepository>(); |
| | 14 | 30 | | var unitOfWork = scope.ServiceProvider.GetRequiredService<IUnitOfWork>(); |
| | | 31 | | |
| | 14 | 32 | | var user = await userRepository.FindByCredentialAsync(settings.User, cancellationToken) |
| | 14 | 33 | | ?? throw new InvalidOperationException( |
| | 14 | 34 | | $"WORKER__USER '{settings.User}' not found in database."); |
| | | 35 | | |
| | 13 | 36 | | if (string.IsNullOrWhiteSpace(settings.RootPath)) |
| | 2 | 37 | | { |
| | 2 | 38 | | throw new InvalidOperationException( |
| | 2 | 39 | | $"WORKER__ROOTPATH is required when WORKER__USER is set, e.g. {StoragePath.OriginalsRoot}/<user>."); |
| | | 40 | | } |
| | | 41 | | |
| | 11 | 42 | | var rootPath = StoragePath.Canonicalize(settings.RootPath) |
| | 11 | 43 | | ?? throw new InvalidOperationException( |
| | 11 | 44 | | $"WORKER__ROOTPATH '{settings.RootPath}' must be an absolute path."); |
| | 10 | 45 | | if (!StoragePath.IsInsideOriginals(rootPath)) |
| | 2 | 46 | | { |
| | 2 | 47 | | throw new InvalidOperationException( |
| | 2 | 48 | | $"WORKER__ROOTPATH '{rootPath}' must be a folder inside {StoragePath.OriginalsRoot}: " + |
| | 2 | 49 | | "use the container path, not the host path."); |
| | | 50 | | } |
| | | 51 | | |
| | 8 | 52 | | var existing = await storageConfigRepository.FindByRootPathAsync(rootPath, cancellationToken); |
| | 8 | 53 | | if (existing is not null) |
| | 3 | 54 | | { |
| | 3 | 55 | | if (existing.UserId != user.Id) |
| | 1 | 56 | | { |
| | 1 | 57 | | throw new InvalidOperationException( |
| | 1 | 58 | | $"Path '{rootPath}' is already assigned to another user."); |
| | | 59 | | } |
| | | 60 | | |
| | 2 | 61 | | Log.StorageConfigExists(logger, user.Username, rootPath); |
| | 2 | 62 | | workerState.ResolvedUserId = user.Id; |
| | 2 | 63 | | return; |
| | | 64 | | } |
| | | 65 | | |
| | 5 | 66 | | var overlapping = StoragePath.FindOverlap( |
| | 5 | 67 | | await storageConfigRepository.GetAllRootPathsAsync(cancellationToken), rootPath); |
| | 5 | 68 | | if (overlapping is not null) |
| | 2 | 69 | | { |
| | 2 | 70 | | throw new InvalidOperationException( |
| | 2 | 71 | | $"Path '{rootPath}' overlaps the existing storage config at '{overlapping}'."); |
| | | 72 | | } |
| | | 73 | | |
| | 3 | 74 | | storageConfigRepository.Add(new UserStorageConfig |
| | 3 | 75 | | { |
| | 3 | 76 | | Id = Guid.NewGuid(), |
| | 3 | 77 | | UserId = user.Id, |
| | 3 | 78 | | RootPath = rootPath, |
| | 3 | 79 | | IsActive = true, |
| | 3 | 80 | | }); |
| | 3 | 81 | | await unitOfWork.SaveChangesAsync(cancellationToken); |
| | | 82 | | |
| | 3 | 83 | | Log.StorageConfigCreated(logger, user.Username, rootPath); |
| | 3 | 84 | | workerState.ResolvedUserId = user.Id; |
| | 7 | 85 | | } |
| | | 86 | | |
| | 1 | 87 | | 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 | | } |