-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(backoffice): projections healthcheck
- Loading branch information
Showing
6 changed files
with
76 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/ParcelRegistry.Projections.BackOffice/HealthCheckRunner.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
namespace ParcelRegistry.Projections.BackOffice | ||
{ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
|
||
public sealed class HealthCheckRunner : BackgroundService | ||
{ | ||
private readonly HealthCheckService _healthCheckService; | ||
private readonly IHostApplicationLifetime _hostApplicationLifetime; | ||
private readonly ILogger<HealthCheckRunner> _logger; | ||
private readonly TimeSpan _checkInterval = TimeSpan.FromSeconds(5); // Check every 5 minutes | ||
|
||
public HealthCheckRunner( | ||
HealthCheckService healthCheckService, | ||
IHostApplicationLifetime hostApplicationLifetime, | ||
ILogger<HealthCheckRunner> logger) | ||
{ | ||
_healthCheckService = healthCheckService; | ||
_hostApplicationLifetime = hostApplicationLifetime; | ||
_logger = logger; | ||
} | ||
|
||
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
{ | ||
await Task.Delay(TimeSpan.FromMinutes(5), stoppingToken).ConfigureAwait(false); | ||
|
||
while (!stoppingToken.IsCancellationRequested) | ||
{ | ||
var report = await _healthCheckService.CheckHealthAsync(stoppingToken); | ||
|
||
// Log the health check result | ||
if (report.Status == HealthStatus.Healthy) | ||
{ | ||
_logger.LogInformation("Database health check passed."); | ||
} | ||
else | ||
{ | ||
_logger.LogError("Database health check failed. Stopping application."); | ||
foreach (var entry in report.Entries) | ||
{ | ||
_logger.LogError($"{entry.Key}: {entry.Value.Status} - {entry.Value.Description}"); | ||
} | ||
|
||
_hostApplicationLifetime.StopApplication(); | ||
} | ||
|
||
await Task.Delay(_checkInterval, stoppingToken); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 0 additions & 56 deletions
56
src/ParcelRegistry.Projections.BackOffice/ProjectionsHealthCheckRunner.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters