| | | 1 | | using Anichron.Core; |
| | | 2 | | using Anichron.Core.Data; |
| | | 3 | | using Anichron.Core.Data.Repository; |
| | | 4 | | using Anichron.Infrastructure.Configuration; |
| | | 5 | | using Anichron.Worker.Crawling; |
| | | 6 | | using Anichron.Worker.Ingestion; |
| | | 7 | | using Anichron.Worker.Ingestion.Middlewares; |
| | | 8 | | using Anichron.Worker.Ingestion.Pipeline; |
| | | 9 | | using Anichron.Worker.Ingestion.Proxy; |
| | | 10 | | using Anichron.Worker.Maintenance; |
| | | 11 | | using Anichron.Worker.Settings; |
| | | 12 | | using Anichron.Worker.Startup; |
| | | 13 | | using Microsoft.EntityFrameworkCore; |
| | | 14 | | using NodaTime; |
| | | 15 | | using System.IO.Abstractions; |
| | | 16 | | using CrawlingWorker = Anichron.Worker.Crawling.Worker; |
| | | 17 | | |
| | | 18 | | namespace Anichron.Worker.Infrastructure; |
| | | 19 | | |
| | | 20 | | internal static class WorkerServiceCollectionExtensions |
| | | 21 | | { |
| | | 22 | | extension(IServiceCollection services) |
| | | 23 | | { |
| | | 24 | | internal IServiceCollection AddWorkerCoreServices() |
| | 3 | 25 | | { |
| | 3 | 26 | | services.AddOptions<WorkerSettings>() |
| | 3 | 27 | | .BindConfiguration("Worker") |
| | 3 | 28 | | .ValidateDataAnnotations() |
| | 3 | 29 | | .ValidateOnStart(); |
| | 3 | 30 | | services.AddSingleton<IGuidFactory, TimeOrderedGuidFactory>(); |
| | 3 | 31 | | services.AddSingleton<IClock>(SystemClock.Instance); |
| | 3 | 32 | | services.AddSingleton<WorkerState>(); |
| | 3 | 33 | | return services; |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | internal IServiceCollection AddWorkerDataServices(IConfiguration configuration) |
| | 0 | 37 | | { |
| | 0 | 38 | | var connectionString = DatabaseConfiguration.GetConnectionString(configuration, new FileSystem()); |
| | 0 | 39 | | services.AddDbContext<AnichronDbContext>(options => |
| | 0 | 40 | | options.UseNpgsql(connectionString, o => o.UseNodaTime())); |
| | 0 | 41 | | services.AddScoped<IUserRepository, EfUserRepository>(); |
| | 0 | 42 | | services.AddScoped<IUserStorageConfigRepository, EfUserStorageConfigRepository>(); |
| | 0 | 43 | | services.AddScoped<IRefreshTokenRepository, EfRefreshTokenRepository>(); |
| | 0 | 44 | | services.AddScoped<IMediaAssetRepository, EfMediaAssetRepository>(); |
| | 0 | 45 | | services.AddScoped<IDatabaseMigrator, EfDatabaseMigrator>(); |
| | 0 | 46 | | services.AddScoped<IUnitOfWork>(sp => sp.GetRequiredService<AnichronDbContext>()); |
| | 0 | 47 | | return services; |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | internal IServiceCollection AddIngestionServices() |
| | 7 | 51 | | { |
| | 7 | 52 | | services.AddSingleton<IFileSystem, FileSystem>(); |
| | 7 | 53 | | services.AddSingleton<ILivePhotoLinker, LivePhotoLinker>(); |
| | 7 | 54 | | services.AddSingleton<IFileIngestionPipeline, FileIngestionPipeline>(); |
| | 7 | 55 | | services.AddSingleton<IProxyDirectoryStrategy, TwoLevelHexShardStrategy>(); |
| | 7 | 56 | | services.AddSingleton<ProxyStagingWriter>(); |
| | 7 | 57 | | services.AddImageProxyServices(); |
| | 7 | 58 | | services.AddVideoProxyServices(); |
| | 7 | 59 | | services.AddScoped<IIngestionMiddleware, LoggingMiddleware>(); |
| | 7 | 60 | | services.AddScoped<IIngestionMiddleware, ContentHashingMiddleware>(); |
| | 7 | 61 | | services.AddScoped<IIngestionMiddleware, IdempotencyCheckMiddleware>(); |
| | 7 | 62 | | services.AddScoped<IIngestionMiddleware, ExifExtractionMiddleware>(); |
| | 7 | 63 | | services.AddScoped<IIngestionMiddleware, ImageProxyMiddleware>(); |
| | 7 | 64 | | services.AddScoped<IIngestionMiddleware, VideoProxyMiddleware>(); |
| | 7 | 65 | | services.AddScoped<IIngestionMiddleware, PersistenceMiddleware>(); |
| | 7 | 66 | | services.AddScoped<IIngestionPipelineRunner, IngestionPipelineRunner>(); |
| | 7 | 67 | | return services; |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | // void, not IServiceCollection: these two are private and both call sites above invoke |
| | | 71 | | // them as statements, so the fluent return was never consumed. The internal members keep |
| | | 72 | | // returning IServiceCollection because they are part of the chainable surface. |
| | | 73 | | private void AddVideoProxyServices() |
| | 7 | 74 | | { |
| | 7 | 75 | | services.AddSingleton<IProcessLauncher, SystemProcessLauncher>(); |
| | 7 | 76 | | services.AddSingleton<IVideoProcessor, FfmpegVideoProcessor>(); |
| | 7 | 77 | | services.AddSingleton<IVideoProxyGenerator, Video720PGenerator>(); |
| | 7 | 78 | | } |
| | | 79 | | |
| | | 80 | | private void AddImageProxyServices() |
| | 7 | 81 | | { |
| | 7 | 82 | | services.AddSingleton<IImageProcessor, ImageSharpProcessor>(); |
| | 7 | 83 | | services.AddSingleton<IImageProxyGenerator, ThumbnailGenerator>(); |
| | 7 | 84 | | services.AddSingleton<IImageProxyGenerator, FullPreviewGenerator>(); |
| | 7 | 85 | | services.AddSingleton<IImageProxyGenerator, BlurhashGenerator>(); |
| | 7 | 86 | | } |
| | | 87 | | |
| | | 88 | | internal IServiceCollection AddWorkerHostedServices() |
| | 1 | 89 | | { |
| | 1 | 90 | | services.AddHostedService<DatabaseMigratorService>(); |
| | 1 | 91 | | services.AddHostedService<WorkerInitializer>(); |
| | 1 | 92 | | services.AddHostedService<TokenCleanupService>(); |
| | 1 | 93 | | services.AddHostedService<CrawlingWorker>(); |
| | 1 | 94 | | return services; |
| | | 95 | | } |
| | | 96 | | } |
| | | 97 | | } |