Files
GmRelayBot/src/GmRelay.Web/Health/NpgsqlHealthCheck.cs
T
Toutsu 3bea327043
PR Checks / test-and-build (pull_request) Successful in 8m53s
feat: add health check endpoints for Bot and Web
- Web: add /health endpoint with PostgreSQL readiness check (returns 200+JSON or 503)
- Web: add /alive endpoint for liveness probe
- Bot: add BotHealthCheckHostedService serving /health on port 8081 via HttpListener
- Bot: expose port 8081 in Dockerfile and install wget for healthcheck
- compose.yaml: add healthcheck sections for bot and web services
- tests: add TDD tests for both health endpoints

Bump version -> 1.16.0

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-13 10:54:22 +03:00

26 lines
832 B
C#

using Microsoft.Extensions.Diagnostics.HealthChecks;
using Npgsql;
namespace GmRelay.Web.Health;
public sealed class NpgsqlHealthCheck(NpgsqlDataSource dataSource) : IHealthCheck
{
public async Task<HealthCheckResult> CheckHealthAsync(
HealthCheckContext context,
CancellationToken cancellationToken = default)
{
try
{
await using var connection = await dataSource.OpenConnectionAsync(cancellationToken);
await using var command = connection.CreateCommand();
command.CommandText = "SELECT 1";
await command.ExecuteScalarAsync(cancellationToken);
return HealthCheckResult.Healthy();
}
catch (Exception ex)
{
return HealthCheckResult.Unhealthy("PostgreSQL is unavailable", ex);
}
}
}