Made a health check api

This commit is contained in:
David Fowler 2023-05-04 15:32:25 -07:00 committed by Reuben Bond
parent 917764273b
commit 3056418c92
2 changed files with 25 additions and 17 deletions

View File

@ -27,23 +27,7 @@ var app = builder.Build();
try
{
app.Logger.LogInformation("Running health checks...");
// Do a health check on startup, this will throw an exception if any of the checks fail
var report = await app.Services.GetRequiredService<HealthCheckService>().CheckHealthAsync();
if (report.Status == HealthStatus.Unhealthy)
{
app.Logger.LogCritical("Health checks failed!");
foreach (var entry in report.Entries)
{
if (entry.Value.Status == HealthStatus.Unhealthy)
{
app.Logger.LogCritical("{Check}: {Status}", entry.Key, entry.Value.Status);
}
}
return 1;
}
await app.CheckHealthAsync();
app.UseServiceDefaults();

View File

@ -66,6 +66,30 @@ public static class CommonExtensions
return app;
}
public static async Task<bool> CheckHealthAsync(this WebApplication app)
{
app.Logger.LogInformation("Running health checks...");
// Do a health check on startup, this will throw an exception if any of the checks fail
var report = await app.Services.GetRequiredService<HealthCheckService>().CheckHealthAsync();
if (report.Status == HealthStatus.Unhealthy)
{
app.Logger.LogCritical("Health checks failed!");
foreach (var entry in report.Entries)
{
if (entry.Value.Status == HealthStatus.Unhealthy)
{
app.Logger.LogCritical("{Check}: {Status}", entry.Key, entry.Value.Status);
}
}
return false;
}
return true;
}
public static IApplicationBuilder UseDefaultOpenApi(this IApplicationBuilder app, IConfiguration configuration)
{
var openApiSection = configuration.GetSection("OpenApi");