Files
components/LiteCharms.Features/HealthChecks/PostgresShopHealthCheck.cs
T
Khwezi Mngoma 7d5e9a18d8
continuous-integration/drone/pr Build is failing
Added MidrandShop feature and spl;it extensions and healthchecks
2026-05-23 11:48:47 +02:00

28 lines
1.0 KiB
C#

using static LiteCharms.Features.Extensions.Postgres;
namespace LiteCharms.Features.HealthChecks;
public class PostgresShopHealthCheck(IConfiguration configuration) : IHealthCheck
{
private readonly string connectionString = configuration.GetConnectionString(ShopDbConfigName)!;
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($"{ShopDbConfigName} is responsive.");
}
catch (Exception ex)
{
return HealthCheckResult.Unhealthy($"{ShopDbConfigName} is unreachable.", ex);
}
}
}