53 lines
1.4 KiB
C#
53 lines
1.4 KiB
C#
|
|
using Microsoft.AspNetCore.Mvc;
|
||
|
|
using Microsoft.EntityFrameworkCore;
|
||
|
|
using StackExchange.Redis;
|
||
|
|
using w4c_workflows.Data;
|
||
|
|
|
||
|
|
namespace w4c_workflows.Controllers;
|
||
|
|
|
||
|
|
[ApiController]
|
||
|
|
public class HealthController : ControllerBase
|
||
|
|
{
|
||
|
|
private readonly WorkflowsDbContext _db;
|
||
|
|
private readonly IConnectionMultiplexer _redis;
|
||
|
|
|
||
|
|
public HealthController(WorkflowsDbContext db, IConnectionMultiplexer redis)
|
||
|
|
{
|
||
|
|
_db = db;
|
||
|
|
_redis = redis;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Liveness — the process is up.</summary>
|
||
|
|
[HttpGet("/health")]
|
||
|
|
public IActionResult Health() => Ok(new { status = "ok" });
|
||
|
|
|
||
|
|
/// <summary>Readiness — Postgres + Redis are reachable.</summary>
|
||
|
|
[HttpGet("/health/ready")]
|
||
|
|
public async Task<IActionResult> Ready(CancellationToken ct)
|
||
|
|
{
|
||
|
|
var checks = new Dictionary<string, string>();
|
||
|
|
|
||
|
|
try
|
||
|
|
{
|
||
|
|
await _db.Database.CanConnectAsync(ct);
|
||
|
|
checks["postgres"] = "ok";
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
checks["postgres"] = $"error: {ex.Message}";
|
||
|
|
}
|
||
|
|
|
||
|
|
try
|
||
|
|
{
|
||
|
|
checks["redis"] = _redis.IsConnected ? "ok" : "disconnected";
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
checks["redis"] = $"error: {ex.Message}";
|
||
|
|
}
|
||
|
|
|
||
|
|
var healthy = checks.Values.All(v => v == "ok");
|
||
|
|
return healthy ? Ok(new { status = "ok", checks }) : StatusCode(503, new { status = "degraded", checks });
|
||
|
|
}
|
||
|
|
}
|