Files
components/LiteCharms.Infrastructure/HealthChecks/PostgresHealthCheck.cs
T
Khwezi Mngoma ff34326a53
continuous-integration/drone/pr Build is passing
Refactored database references
2026-05-09 16:58:34 +02:00

26 lines
980 B
C#

namespace LiteCharms.Infrastructure.HealthChecks;
public class PostgresHealthCheck(IConfiguration configuration) : IHealthCheck
{
private readonly string connectionString = configuration.GetConnectionString("PostgresShop")!;
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
try
{
await using var dataSource = NpgsqlDataSource.Create(connectionString);
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("PostgreSQL is responsive.");
}
catch (Exception ex)
{
return HealthCheckResult.Unhealthy("PostgreSQL is unreachable.", ex);
}
}
}