using System.Threading.Channels; using Cleanuparr.Domain.Entities.Arr; using Cleanuparr.Infrastructure.Features.DownloadRemover.Consumers; using Cleanuparr.Infrastructure.Features.DownloadRemover.Models; using Cleanuparr.Infrastructure.Features.Messaging; using Cleanuparr.Infrastructure.Features.Notifications.Consumers; using Cleanuparr.Infrastructure.Features.Notifications.Models; using Cleanuparr.Infrastructure.Health; using Cleanuparr.Infrastructure.Http; using Cleanuparr.Infrastructure.Http.DynamicHttpClientSystem; using Cleanuparr.Shared.Helpers; using Microsoft.Extensions.Caching.Memory; namespace Cleanuparr.Api.DependencyInjection; public static class MainDI { public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration) => services .AddHttpClients(configuration) .AddSingleton() .AddSingleton(serviceProvider => serviceProvider.GetRequiredService()) .AddServices() .AddHealthServices() .AddQuartzServices(configuration) .AddNotifications() .AddMessageQueue() .AddMessageQueue(); /// /// Registers an unbounded in-process queue for , /// drained one message at a time by a . /// private static IServiceCollection AddMessageQueue(this IServiceCollection services) where THandler : class, IMessageHandler { Channel channel = Channel.CreateUnbounded(new UnboundedChannelOptions { SingleReader = true }); return services .AddSingleton(channel) .AddSingleton(channel.Writer) .AddScoped, THandler>() .AddHostedService>(); } private static IServiceCollection AddHttpClients(this IServiceCollection services, IConfiguration configuration) { // Add the dynamic HTTP client system - this replaces all the previous static configurations services.AddDynamicHttpClients(); // Add the dynamic HTTP client provider that uses the new system services.AddSingleton(); return services; } /// /// Adds health check services to the service collection /// private static IServiceCollection AddHealthServices(this IServiceCollection services) => services // Register the existing health check service for download clients .AddSingleton() // Register the background service for periodic health checks .AddHostedService() // Add ASP.NET Core health checks .AddHealthChecks() .AddCheck("application", tags: ["liveness"]) .AddCheck("database", tags: ["readiness"]) .AddCheck("filesystem", tags: ["readiness"]) .AddCheck("download_clients", tags: ["readiness"]) .Services; }