fix(startup): Hangfire toujours actif en prod, migrations auto, crash boucle corrige
- Hangfire: retire le flag Hangfire:Enabled (toujours actif si DATABASE_URL\n est defini, skip automatiquement en test sans config)\n- Database:MigrateOnStartup: default true en Production (.IsProduction())\n Plus besoin de variable d'env pour que les migrations s'executent\n- Recurring jobs registers dans if(connectionString) pour eviter le\n crash 'No service for IRecurringJobManager' quand Hangfire est skip\n- Corrige le crash boucle 'relation roles does not exist' apres reset DB\n\nBuild: 0 errors, 0 warnings\nTests: 226/226 passed
This commit is contained in:
1 parent
7a02a87e4c
commit
27b6025565
1 file changed
+12
-13
+12
-13
@@ -181,18 +181,14 @@ builder.Services.AddApplication();
|
||||
// ── Infrastructure layer ──────────────────────────────────────────
|
||||
builder.Services.AddInfrastructure(builder.Configuration);
|
||||
|
||||
// ── Database connection string (for Hangfire) ─────────────────────
|
||||
var hangfireEnabled = builder.Configuration.GetValue<bool>("Hangfire:Enabled");
|
||||
if (hangfireEnabled)
|
||||
{
|
||||
var connectionString = Environment.GetEnvironmentVariable("DATABASE_URL")
|
||||
?? builder.Configuration.GetConnectionString("Default")
|
||||
?? throw new InvalidOperationException(
|
||||
"DATABASE_URL is not configured. Set the DATABASE_URL environment variable " +
|
||||
"or configure a Default connection string in appsettings.json. " +
|
||||
"Example: Host=host;Port=5432;Database=heimdall;Username=user;Password=password");
|
||||
// ── Hangfire (required: heartbeat monitor, agent failover, etc.) ──
|
||||
// Enabled automatically when DATABASE_URL is set (production).
|
||||
// Tests use InMemory and don't set DATABASE_URL — Hangfire is skipped there.
|
||||
var connectionString = Environment.GetEnvironmentVariable("DATABASE_URL")
|
||||
?? builder.Configuration.GetConnectionString("Default");
|
||||
|
||||
// ── Hangfire ──────────────────────────────────────────────────
|
||||
if (!string.IsNullOrEmpty(connectionString))
|
||||
{
|
||||
#pragma warning disable CS0618
|
||||
builder.Services.AddHangfire(config =>
|
||||
{
|
||||
@@ -209,7 +205,7 @@ if (hangfireEnabled)
|
||||
options.Queues = ["default", "heartbeat"];
|
||||
});
|
||||
|
||||
// ── Jobs ──────────────────────────────────────────────────────
|
||||
// ── Jobs ──────────────────────────────────────────────────────────
|
||||
builder.Services.AddScoped<HeartbeatMonitorJob>();
|
||||
builder.Services.AddScoped<AgentFailoverJob>();
|
||||
}
|
||||
@@ -406,8 +402,11 @@ if (migrateOnStartup)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ── Register recurring Hangfire jobs ─────────────────────────────────
|
||||
// ── Register recurring Hangfire jobs (heartbeat monitor, agent failover) ──
|
||||
if (!string.IsNullOrEmpty(connectionString))
|
||||
{
|
||||
using (var scope = app.Services.CreateScope())
|
||||
{
|
||||
var manager = scope.ServiceProvider.GetRequiredService<IRecurringJobManager>();
|
||||
|
||||
Reference in new issue
Block a user