| | 1 | | using Microsoft.Extensions.Diagnostics.HealthChecks; |
| | 2 | | using Microsoft.Extensions.Hosting; |
| | 3 | | using Microsoft.Extensions.Logging; |
| | 4 | |
|
| | 5 | | namespace Infrastructure.Service; |
| | 6 | | public class HealthCheckResilienceService : IHostedService { |
| | 7 | | private readonly ILogger<HealthCheckResilienceService>? _logger; |
| | 8 | | private readonly HealthCheckService _service; |
| | 9 | |
|
| 0 | 10 | | public HealthCheckResilienceService(ILogger<HealthCheckResilienceService>? logger, HealthCheckService service) { |
| 0 | 11 | | _logger = logger; |
| 0 | 12 | | _service = service; |
| 0 | 13 | | } |
| | 14 | |
|
| | 15 | | private const int MAX_RETRIES = 10; |
| | 16 | | private const int RETRY_DELAY = 5; |
| | 17 | |
|
| 0 | 18 | | public async Task WaitForHealthyStatusAsync(CancellationToken ct = default) { |
| 0 | 19 | | var isHealthy = false; |
| | 20 | |
|
| 0 | 21 | | for (int attempt = 1; attempt <= MAX_RETRIES; attempt++) { |
| 0 | 22 | | try { |
| 0 | 23 | | var healthReport = await _service.CheckHealthAsync(ct); |
| | 24 | |
|
| 0 | 25 | | if (healthReport.Status == HealthStatus.Healthy) { |
| 0 | 26 | | _logger?.LogInformation("Health check passed on attempt {Attempt}.", attempt); |
| 0 | 27 | | isHealthy = true; |
| 0 | 28 | | break; |
| | 29 | | } |
| | 30 | |
|
| 0 | 31 | | _logger?.LogWarning( |
| 0 | 32 | | "Health check failed on attempt {Attempt}/{MaxRetries}. Retrying in {RetryDelaySeconds} seconds...", |
| 0 | 33 | | attempt, |
| 0 | 34 | | MAX_RETRIES, |
| 0 | 35 | | RETRY_DELAY |
| 0 | 36 | | ); |
| | 37 | |
|
| 0 | 38 | | foreach (var entry in healthReport.Entries) { |
| 0 | 39 | | _logger?.LogWarning( |
| 0 | 40 | | "Health check entry {Key}: {Status} - {Description}", |
| 0 | 41 | | entry.Key, |
| 0 | 42 | | entry.Value.Status, |
| 0 | 43 | | entry.Value.Description |
| 0 | 44 | | ); |
| 0 | 45 | | } |
| 0 | 46 | | } |
| 0 | 47 | | catch (Exception ex) { |
| 0 | 48 | | _logger?.LogError( |
| 0 | 49 | | ex, |
| 0 | 50 | | "An exception occurred during health check attempt {Attempt}/{MaxRetries}.", |
| 0 | 51 | | attempt, |
| 0 | 52 | | MAX_RETRIES |
| 0 | 53 | | ); |
| 0 | 54 | | } |
| | 55 | |
|
| 0 | 56 | | for (int remainingSeconds = RETRY_DELAY; remainingSeconds > 0; remainingSeconds--) { |
| 0 | 57 | | _logger?.LogInformation("Retrying in {RemainingSeconds} second(s)...", remainingSeconds); |
| 0 | 58 | | await Task.Delay(TimeSpan.FromSeconds(1), ct); |
| 0 | 59 | | } |
| 0 | 60 | | } |
| | 61 | |
|
| 0 | 62 | | if (!isHealthy) { |
| 0 | 63 | | _logger?.LogError("-------------------------------------------------------------------------------"); |
| 0 | 64 | | _logger?.LogError("Health check failed after {MaxRetries:D2} attempts. Application starting as not operational!", |
| 0 | 65 | | _logger?.LogError("-------------------------------------------------------------------------------"); |
| 0 | 66 | | }; |
| 0 | 67 | | } |
| | 68 | |
|
| 0 | 69 | | public async Task RunManuallyAsync(CancellationToken ct = default) { |
| 0 | 70 | | await StartAsync(ct); |
| 0 | 71 | | } |
| | 72 | |
|
| 0 | 73 | | public async Task StartAsync(CancellationToken ct) { |
| 0 | 74 | | await WaitForHealthyStatusAsync(ct); |
| 0 | 75 | | } |
| | 76 | |
|
| 0 | 77 | | public Task StopAsync(CancellationToken ct) { |
| 0 | 78 | | throw new NotImplementedException(); |
| | 79 | | } |
| | 80 | | } |