| | | 1 | | using Anichron.Core; |
| | | 2 | | using Anichron.Core.Data; |
| | | 3 | | using Anichron.Core.Data.Repository; |
| | | 4 | | using Anichron.Core.Work; |
| | | 5 | | using Anichron.Infrastructure.Configuration; |
| | | 6 | | using Anichron.Worker.Bursts; |
| | | 7 | | using Anichron.Worker.CompactPreviews; |
| | | 8 | | using Anichron.Worker.Crawling; |
| | | 9 | | using Anichron.Worker.Ingestion; |
| | | 10 | | using Anichron.Worker.Ingestion.Classification; |
| | | 11 | | using Anichron.Worker.Ingestion.Extraction; |
| | | 12 | | using Anichron.Worker.Ingestion.Middlewares; |
| | | 13 | | using Anichron.Worker.Ingestion.Pipeline; |
| | | 14 | | using Anichron.Worker.Ingestion.Proxy; |
| | | 15 | | using Anichron.Worker.Maintenance; |
| | | 16 | | using Anichron.Worker.ProxySweeps; |
| | | 17 | | using Anichron.Worker.Settings; |
| | | 18 | | using Anichron.Worker.Startup; |
| | | 19 | | using Anichron.Worker.Work; |
| | | 20 | | using Microsoft.Extensions.Options; |
| | | 21 | | using NodaTime; |
| | | 22 | | using System.IO.Abstractions; |
| | | 23 | | |
| | | 24 | | namespace Anichron.Worker.Infrastructure; |
| | | 25 | | |
| | | 26 | | internal static class WorkerServiceCollectionExtensions |
| | | 27 | | { |
| | | 28 | | extension(IServiceCollection services) |
| | | 29 | | { |
| | | 30 | | internal IServiceCollection AddWorkerCoreServices() |
| | 7 | 31 | | { |
| | 7 | 32 | | services.AddOptions<WorkerSettings>() |
| | 7 | 33 | | .BindConfiguration("Worker") |
| | 7 | 34 | | .ValidateDataAnnotations() |
| | 7 | 35 | | .ValidateOnStart(); |
| | 7 | 36 | | services.AddSingleton<IGuidFactory, TimeOrderedGuidFactory>(); |
| | 7 | 37 | | services.AddSingleton<IClock>(SystemClock.Instance); |
| | 7 | 38 | | services.AddSingleton(DateTimeZoneProviders.Tzdb); |
| | 7 | 39 | | services.AddSingleton(DateTimeZoneProviders.Tzdb.GetSystemDefault()); |
| | 7 | 40 | | services.AddSingleton(TimeProvider.System); |
| | 7 | 41 | | services.AddSingleton<WorkerState>(); |
| | 7 | 42 | | return services; |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | internal IServiceCollection AddWorkerDataServices(IConfiguration configuration) |
| | 13 | 46 | | { |
| | 13 | 47 | | services.AddDatabaseSettings(configuration).AddDbContextFactory<AnichronDbContext>((provider, options) => |
| | 13 | 48 | | options.UseAnichronNpgsql(provider.GetRequiredService<IOptions<DatabaseConnectionSettings>>().Value.ToCo |
| | 13 | 49 | | services.AddScoped<IUserRepository, EfUserRepository>(); |
| | 13 | 50 | | services.AddScoped<IUserStorageConfigRepository, EfUserStorageConfigRepository>(); |
| | 13 | 51 | | services.AddScoped<IRefreshTokenRepository, EfRefreshTokenRepository>(); |
| | 13 | 52 | | services.AddScoped<IMediaAssetRepository, EfMediaAssetRepository>(); |
| | 13 | 53 | | services.AddScoped<IBurstRepository, EfBurstRepository>(); |
| | 13 | 54 | | services.AddScoped<IProxyOrphanStore, EfProxyOrphanStore>(); |
| | 13 | 55 | | services.AddScoped<ActiveStorageConfigs>(); |
| | 13 | 56 | | services.AddScoped<IUnitOfWork>(sp => sp.GetRequiredService<AnichronDbContext>()); |
| | 13 | 57 | | services.AddScoped<IWorkQueue, EfWorkQueue>(); |
| | 13 | 58 | | services.AddSingleton<IWorkItemStore, EfWorkItemStore>(); |
| | 13 | 59 | | return services; |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | internal IServiceCollection AddWorkQueueServices() |
| | 4 | 63 | | { |
| | 4 | 64 | | services.AddSingleton<WorkHandlerRegistry>(); |
| | 4 | 65 | | services.AddWorkHandler<TokenCleanupPayload, TokenCleanupHandler>(); |
| | 4 | 66 | | services.AddWorkHandler<WorkItemRetentionPayload, WorkItemRetentionHandler>(); |
| | 4 | 67 | | services.AddWorkHandler<CrawlPayload, CrawlHandler>(); |
| | 4 | 68 | | services.AddWorkHandler<BurstDetectionPayload, BurstDetectionHandler>(); |
| | 4 | 69 | | services.AddWorkHandler<CompactPreviewPayload, CompactPreviewHandler>(); |
| | 4 | 70 | | services.AddWorkHandler<ProxySweepPayload, ProxySweepHandler>(); |
| | 4 | 71 | | services.AddWorkHandler<ProxyDeletionPayload, ProxyDeletionHandler>(); |
| | 4 | 72 | | services.AddSingleton<IWorkScheduleSource, TokenCleanupSchedule>(); |
| | 4 | 73 | | services.AddSingleton<IWorkScheduleSource, WorkItemRetentionSchedule>(); |
| | 4 | 74 | | services.AddSingleton<IWorkScheduleSource, StorageConfigSchedules<CrawlPayload>>(); |
| | 4 | 75 | | services.AddSingleton<IWorkScheduleSource, StorageConfigSchedules<BurstDetectionPayload>>(); |
| | 4 | 76 | | services.AddSingleton<IWorkScheduleSource, StorageConfigSchedules<CompactPreviewPayload>>(); |
| | 4 | 77 | | return services; |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | private void AddWorkHandler<TPayload, THandler>() |
| | | 81 | | where TPayload : IWorkPayload |
| | | 82 | | where THandler : class, IWorkHandler<TPayload> |
| | 28 | 83 | | { |
| | 28 | 84 | | services.AddScoped<IWorkHandler<TPayload>, THandler>(); |
| | 28 | 85 | | services.AddSingleton(WorkHandlerRegistration.For<TPayload>()); |
| | 28 | 86 | | } |
| | | 87 | | |
| | | 88 | | internal IServiceCollection AddIngestionServices() |
| | 30 | 89 | | { |
| | 30 | 90 | | services.AddSingleton<IFileSystem, FileSystem>(); |
| | 30 | 91 | | services.AddSingleton<ILivePhotoLinker, LivePhotoLinker>(); |
| | 30 | 92 | | services.AddSingleton<CrawlExclusions>(); |
| | 30 | 93 | | services.AddSingleton<IFileIngestionPipeline, FileIngestionPipeline>(); |
| | 30 | 94 | | services.AddScoped<ISharedContentReporter, SharedContentReporter>(); |
| | 30 | 95 | | services.AddScoped<IMissingFileSweep, MissingFileSweep>(); |
| | 30 | 96 | | services.AddSingleton<IOriginalPhotoReader, OriginalPhotoReader>(); |
| | 30 | 97 | | services.AddScoped<IThumbnailMatcher, OrbThumbnailMatcher>(); |
| | 30 | 98 | | services.AddSingleton<IProxyDirectoryStrategy, TwoLevelHexShardStrategy>(); |
| | 30 | 99 | | services.AddSingleton<ProxyStagingWriter>(); |
| | 30 | 100 | | services.AddImageProxyServices(); |
| | 30 | 101 | | services.AddVideoProxyServices(); |
| | 30 | 102 | | services.AddScoped<PairedMovResolver>(); |
| | 30 | 103 | | services.AddScoped<PairedMovProxyWriter>(); |
| | 30 | 104 | | services.AddScoped<IngestionSaver>(); |
| | 30 | 105 | | services.AddScoped<IngestionClassifier>(); |
| | 30 | 106 | | services.AddScoped<LivePhotoPairer>(); |
| | 30 | 107 | | services.AddScoped<StoredContent>(); |
| | 30 | 108 | | services.AddSingleton<CaptureZones>(); |
| | 30 | 109 | | services.AddScoped<KnownFileReconciler>(); |
| | 30 | 110 | | services.AddScoped<IOutcomeWriter, NewContentWriter>(); |
| | 30 | 111 | | services.AddScoped<IOutcomeWriter, ContentChangedWriter>(); |
| | 30 | 112 | | services.AddScoped<IIngestionMiddleware, LoggingMiddleware>(); |
| | 30 | 113 | | services.AddScoped<IIngestionMiddleware, ContentHashingMiddleware>(); |
| | 30 | 114 | | services.AddScoped<IIngestionMiddleware, ClassificationMiddleware>(); |
| | 30 | 115 | | services.AddScoped<IIngestionMiddleware, MetadataExtractionMiddleware>(); |
| | 30 | 116 | | services.AddScoped<IIngestionMiddleware, ImageProxyMiddleware>(); |
| | 30 | 117 | | services.AddScoped<IIngestionMiddleware, VideoProxyMiddleware>(); |
| | 30 | 118 | | services.AddScoped<IIngestionMiddleware, PersistenceMiddleware>(); |
| | 30 | 119 | | services.AddScoped<IIngestionPipelineRunner, IngestionPipelineRunner>(); |
| | 30 | 120 | | return services; |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | private void AddVideoProxyServices() |
| | 30 | 124 | | { |
| | 30 | 125 | | services.AddSingleton<IProcessLauncher, SystemProcessLauncher>(); |
| | 30 | 126 | | services.AddSingleton<IVideoProcessor, FfmpegVideoProcessor>(); |
| | 30 | 127 | | services.AddSingleton<IVideoFrameExtractor, FfmpegFrameExtractor>(); |
| | 30 | 128 | | services.AddSingleton<IVideoProxyGenerator, WebVideoGenerator>(); |
| | 30 | 129 | | } |
| | | 130 | | |
| | | 131 | | private void AddImageProxyServices() |
| | 30 | 132 | | { |
| | 30 | 133 | | services.AddSingleton<MagickHeifDecoder>(); |
| | 30 | 134 | | services.AddSingleton<ImageSharpUprightDecoder>(); |
| | 30 | 135 | | services.AddSingleton<IUprightImageDecoder, UprightImageDecoder>(); |
| | 30 | 136 | | services.AddSingleton<IUprightImageSource, UprightImageSource>(); |
| | 30 | 137 | | services.AddSingleton<IImageProcessor, ImageSharpProcessor>(); |
| | 30 | 138 | | services.AddSingleton<IImageProxyGenerator, ThumbnailGenerator>(); |
| | 30 | 139 | | services.AddSingleton<IImageProxyGenerator, FullPreviewGenerator>(); |
| | 30 | 140 | | services.AddSingleton<BlurhashEncoder>(); |
| | 30 | 141 | | services.AddSingleton<ICompactPreviewEncoder, MagickCompactPreviewEncoder>(); |
| | 30 | 142 | | } |
| | | 143 | | |
| | | 144 | | internal IServiceCollection AddWorkerHostedServices() |
| | 2 | 145 | | { |
| | 2 | 146 | | services.AddHostedService<WorkerInitializer>(); |
| | 2 | 147 | | services.AddHostedService<WorkScheduler>(); |
| | 2 | 148 | | services.AddHostedService<WorkExecutor>(); |
| | 2 | 149 | | return services; |
| | | 150 | | } |
| | | 151 | | } |
| | | 152 | | } |