using FictionArchive.Service.SchedulerService.GraphQL; using FictionArchive.Service.SchedulerService.Services; using FictionArchive.Service.Shared; using FictionArchive.Service.Shared.Extensions; using FictionArchive.Service.Shared.Services.EventBus.Implementations; using Quartz; using Quartz.Impl.AdoJobStore; namespace FictionArchive.Service.SchedulerService; public class Program { public static void Main(string[] args) { var isSchemaExport = SchemaExportDetector.IsSchemaExportMode(args); var builder = WebApplication.CreateBuilder(args); // Services builder.Services.AddDefaultGraphQl(); builder.Services.AddHealthChecks(); builder.Services.AddTransient(); #region Database builder.Services.RegisterDbContext( builder.Configuration.GetConnectionString("DefaultConnection"), skipInfrastructure: isSchemaExport); #endregion #region Event Bus if (!isSchemaExport) { builder.Services.AddRabbitMQ(opt => { builder.Configuration.GetSection("RabbitMQ").Bind(opt); }); } #endregion #region Quartz if (isSchemaExport) { // Schema export mode: use in-memory store (no DB connection needed) builder.Services.AddQuartz(opt => { opt.UseInMemoryStore(); }); } else { builder.Services.AddQuartz(opt => { opt.UsePersistentStore(pso => { pso.UsePostgres(pgsql => { pgsql.ConnectionString = builder.Configuration.GetConnectionString("DefaultConnection"); pgsql.UseDriverDelegate(); pgsql.TablePrefix = "quartz.qrtz_"; // Needed for Postgres due to the differing schema used }); pso.UseNewtonsoftJsonSerializer(); }); }); builder.Services.AddQuartzHostedService(opt => { opt.WaitForJobsToComplete = true; }); } #endregion var app = builder.Build(); // Update database (skip in schema export mode) if (!isSchemaExport) { using var scope = app.Services.CreateScope(); var dbContext = scope.ServiceProvider.GetRequiredService(); dbContext.UpdateDatabase(); } app.UseHttpsRedirection(); app.MapHealthChecks("/healthz"); app.MapGraphQL(); app.RunWithGraphQLCommands(args); } }