| | | 1 | | using Anichron.Core.Data; |
| | | 2 | | using Anichron.Core.Data.Repository; |
| | | 3 | | using Anichron.Core.Domain; |
| | | 4 | | using Anichron.Worker.Crawling; |
| | | 5 | | using Anichron.Worker.Settings; |
| | | 6 | | using Microsoft.Extensions.Options; |
| | | 7 | | |
| | | 8 | | namespace Anichron.Worker.Startup; |
| | | 9 | | |
| | 8 | 10 | | public sealed partial class WorkerInitializer( |
| | 8 | 11 | | IOptions<WorkerSettings> options, |
| | 8 | 12 | | IServiceScopeFactory scopeFactory, |
| | 8 | 13 | | WorkerState workerState, |
| | 8 | 14 | | ILogger<WorkerInitializer> logger) : IHostedService |
| | | 15 | | { |
| | 8 | 16 | | private readonly WorkerSettings settings = options.Value; |
| | | 17 | | |
| | | 18 | | public async Task StartAsync(CancellationToken cancellationToken) |
| | 7 | 19 | | { |
| | 7 | 20 | | if (string.IsNullOrWhiteSpace(settings.User)) |
| | 2 | 21 | | { |
| | 2 | 22 | | Log.AllUserMode(logger); |
| | 2 | 23 | | return; |
| | | 24 | | } |
| | | 25 | | |
| | 5 | 26 | | await using var scope = scopeFactory.CreateAsyncScope(); |
| | 5 | 27 | | var userRepository = scope.ServiceProvider.GetRequiredService<IUserRepository>(); |
| | 5 | 28 | | var storageConfigRepository = scope.ServiceProvider.GetRequiredService<IUserStorageConfigRepository>(); |
| | 5 | 29 | | var unitOfWork = scope.ServiceProvider.GetRequiredService<IUnitOfWork>(); |
| | | 30 | | |
| | 5 | 31 | | var credential = settings.User.Trim().ToLowerInvariant(); |
| | 5 | 32 | | var user = await userRepository.FindByCredentialAsync(credential, cancellationToken) |
| | 5 | 33 | | ?? throw new InvalidOperationException( |
| | 5 | 34 | | $"WORKER__USER '{settings.User}' not found in database."); |
| | | 35 | | |
| | 4 | 36 | | var existing = await storageConfigRepository.FindByRootPathAsync(settings.RootPath, cancellationToken); |
| | 4 | 37 | | if (existing is not null) |
| | 3 | 38 | | { |
| | 3 | 39 | | if (existing.UserId != user.Id) |
| | 1 | 40 | | { |
| | 1 | 41 | | throw new InvalidOperationException( |
| | 1 | 42 | | $"Path '{settings.RootPath}' is already assigned to another user."); |
| | | 43 | | } |
| | | 44 | | |
| | 2 | 45 | | Log.StorageConfigExists(logger, user.Username, settings.RootPath); |
| | 2 | 46 | | workerState.ResolvedUserId = user.Id; |
| | 2 | 47 | | return; |
| | | 48 | | } |
| | | 49 | | |
| | 1 | 50 | | storageConfigRepository.Add(new UserStorageConfig |
| | 1 | 51 | | { |
| | 1 | 52 | | Id = Guid.NewGuid(), |
| | 1 | 53 | | UserId = user.Id, |
| | 1 | 54 | | RootPath = settings.RootPath, |
| | 1 | 55 | | IsActive = true, |
| | 1 | 56 | | }); |
| | 1 | 57 | | await unitOfWork.SaveChangesAsync(cancellationToken); |
| | | 58 | | |
| | 1 | 59 | | Log.StorageConfigCreated(logger, user.Username, settings.RootPath); |
| | 1 | 60 | | workerState.ResolvedUserId = user.Id; |
| | 5 | 61 | | } |
| | | 62 | | |
| | 1 | 63 | | 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 | | } |