Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
arealmaas committed Nov 5, 2024
1 parent eef3206 commit a7401a2
Showing 1 changed file with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
using Microsoft.Extensions.Diagnostics.HealthChecks;
using StackExchange.Redis;
using Microsoft.Extensions.Options;

using Microsoft.Extensions.Logging;
namespace Digdir.Domain.Dialogporten.Infrastructure.HealthChecks;

internal sealed class RedisHealthCheck : IHealthCheck
{
private readonly InfrastructureSettings _settings;
private readonly ILogger<RedisHealthCheck> _logger;

public RedisHealthCheck(IOptions<InfrastructureSettings> options)
public RedisHealthCheck(IOptions<InfrastructureSettings> options, ILogger<RedisHealthCheck> logger)
{
_settings = options?.Value ?? throw new ArgumentNullException(nameof(options));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
var sw = System.Diagnostics.Stopwatch.StartNew();
try
{
var timeout = 15000;
var sw = System.Diagnostics.Stopwatch.StartNew();

var options = ConfigurationOptions.Parse(_settings.Redis.ConnectionString);

Expand All @@ -29,26 +31,33 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context
using var redis = await ConnectionMultiplexer.ConnectAsync(options);
var db = redis.GetDatabase();
await db.PingAsync();
sw.Stop();

if (sw.Elapsed > TimeSpan.FromSeconds(5))
{
_logger.LogWarning("Redis connection is slow ({Elapsed:N1}s).", sw.Elapsed.TotalSeconds);
return HealthCheckResult.Degraded($"Redis connection is slow ({sw.Elapsed.TotalSeconds:N1}s).");
}

return HealthCheckResult.Healthy("Redis connection is healthy.");
}
catch (RedisTimeoutException ex)
{
_logger.LogWarning("Redis connection timed out ({Elapsed:N1}s).", sw.Elapsed.TotalSeconds);
return HealthCheckResult.Unhealthy("Redis connection timed out.", exception: ex);
}
catch (RedisConnectionException ex)
{
_logger.LogWarning(ex, "Unable to connect to Redis.");
return HealthCheckResult.Unhealthy("Unable to connect to Redis.", exception: ex);
}
catch (Exception ex)
{
_logger.LogError(ex, "An unexpected error occurred while checking Redis health.");
return HealthCheckResult.Unhealthy("An unexpected error occurred while checking Redis health.", exception: ex);
}
finally
{
sw.Stop();
}
}
}

0 comments on commit a7401a2

Please sign in to comment.