using Hangfire; using Hangfire.PostgreSql; using OpenShock.Common; using OpenShock.Common.Extensions; using OpenShock.Cron; using OpenShock.Cron.Services.Email; using OpenShock.Cron.Utils; var builder = OpenShockApplication.CreateDefaultBuilder(args); var redisOptions = builder.RegisterRedisOptions(); var databaseOptions = builder.RegisterDatabaseOptions(); builder.RegisterMetricsOptions(); // The outbox dispatcher builds activation/reset links, so it needs the frontend base URL. builder.RegisterFrontendOptions(); builder.Services.AddOpenShockMemDB(redisOptions); builder.Services.AddOpenShockDB(databaseOptions); builder.Services.AddOpenShockServices(); // XML documentation comments are applied by a source generator that intercepts this call, so it has to be made here in the host builder.Services.AddOpenApi(OpenShockServiceHelper.OpenApiDocumentName, OpenShockServiceHelper.ConfigureOpenApi); // Hangfire workers fetch enqueued jobs by polling the queue table. The default interval (15s) is // left as-is in production - email delivery within that window is fine and it adds no DB load. Only // the integration test overrides it (via OpenShock:Hangfire:QueuePollInterval) to run fast. var hangfireStorageOptions = new PostgreSqlStorageOptions(); if (builder.Configuration.GetValue("OpenShock:Hangfire:QueuePollInterval") is { } queuePollInterval) hangfireStorageOptions.QueuePollInterval = queuePollInterval; builder.Services.AddHangfire(hangfire => hangfire.UsePostgreSqlStorage( c => c.UseNpgsqlConnection(databaseOptions.Conn), hangfireStorageOptions)); builder.Services.AddHangfireServer(); // Registers the email providers, the outbox dispatcher, and the Redis notification listener. Delivery // is the EmailOutboxDeliveryJob, driven through Hangfire (recurring every-minute sweep auto-registered // via [CronJob], plus on-demand enqueue from the listener); all retry/lease/state lives on the // email_outbox row, not in Hangfire. The API host only writes outbox rows; all sending happens here. await builder.AddEmailService(); var app = builder.Build(); await app.UseCommonOpenShockMiddleware(); // The Cron host does not own migrations (the API is the sole migrator). Its OpenShockContext binds // Postgres enum types by name at the pooled data source's first connection and caches them for the // process's life, so it must not open that context before a newly-added enum exists - otherwise every // claim query fails permanently ("data type name 'email_status' could not be found"). Block until the // migrator has applied all pending migrations, which happens before Hangfire or any job runs below. await app.WaitForOpenShockSchemaReady(databaseOptions); var hangfireOptions = new DashboardOptions(); if (app.Environment.IsProduction() || Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER") == "true") { hangfireOptions.Authorization = [ ]; hangfireOptions.AsyncAuthorization = [ new DashboardAdminAuth() ]; } app.UseHangfireDashboard(options: hangfireOptions); var jobManager = app.Services.GetRequiredService(); foreach (var cronJob in CronJobCollector.GetAllCronJobs()) { jobManager.AddOrUpdate(cronJob.Name, cronJob.Job, cronJob.Schedule); } await app.RunAsync(); // Expose Program for integration tests (so the test host can boot the Cron pipeline in-process). public partial class Program;