Fix API deploy healthcheck by starting Kestrel before migrate/seed.
CI / backend (push) Canceled after 0s
CI / frontend (push) Canceled after 0s

Compose was marking the API unhealthy because bootstrap blocked app.Run(), so /health/live never answered within the HEALTHCHECK window.
This commit is contained in:
2026-10-04 19:27:21 +08:00
parent d013dd4bb5
commit 7d7332641f
9 changed files with 915 additions and 22 deletions
+13 -17
View File
@@ -6,10 +6,8 @@ using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.RateLimiting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using Minio;
using Minio.DataModel.Args;
using Prometheus;
using Serilog;
using StackExchange.Redis;
@@ -148,8 +146,10 @@ try
builder.Services.AddHostedService<MetricsCollectorService>();
builder.Services.AddHostedService<PromotionRetryService>();
// Health checks
builder.Services.AddHealthChecks()
// Health checks. Startup bootstrap (migrate/seed/MinIO) runs in a hosted
// service AFTER Kestrel listens so Docker's /health/live probe can pass;
// /health/ready stays gated until StartupHealthCheck flips.
var healthChecks = builder.Services.AddHealthChecks()
.AddNpgSql(
builder.Configuration.GetConnectionString("DefaultConnection")!,
name: "postgresql",
@@ -162,6 +162,15 @@ try
"minio",
tags: new[] { "ready" });
if (!isTesting)
{
builder.Services.AddSingleton<StartupHealthCheck>();
builder.Services.AddHostedService<AppBootstrapHostedService>();
healthChecks.AddCheck<StartupHealthCheck>(
"startup",
tags: new[] { "ready", "startup" });
}
builder.Services.AddValidatorsFromAssemblyContaining<Program>();
builder.Services.AddScoped<ValidationFilter>();
builder.Services.AddControllers(options =>
@@ -229,19 +238,6 @@ try
}
});
if (!app.Environment.IsEnvironment("Testing"))
{
using var scope = app.Services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
await db.Database.MigrateAsync();
await DataSeeder.SeedAsync(db);
var minio = scope.ServiceProvider.GetRequiredService<IMinioClient>();
var minioOpts = scope.ServiceProvider.GetRequiredService<IOptions<MinioOptions>>().Value;
if (!await minio.BucketExistsAsync(new BucketExistsArgs().WithBucket(minioOpts.BucketName)))
await minio.MakeBucketAsync(new MakeBucketArgs().WithBucket(minioOpts.BucketName));
}
app.Run();
}
catch (System.Exception)