Skip to content

Commit

Permalink
refactor(backoffice): projections healthcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
ArneD committed Oct 15, 2024
1 parent 2fd99af commit 5b1bd1f
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 59 deletions.
2 changes: 1 addition & 1 deletion paket.dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ nuget Be.Vlaanderen.Basisregisters.ProjectionHandling.Connector.Testing 14.0.0
nuget Be.Vlaanderen.Basisregisters.ProjectionHandling.Testing.Xunit 14.0.0
nuget Be.Vlaanderen.Basisregisters.ProjectionHandling.Syndication 14.0.0

nuget Be.Vlaanderen.Basisregisters.Projector 15.0.0
nuget Be.Vlaanderen.Basisregisters.Projector 15.1.0

nuget Be.Vlaanderen.Basisregisters.Crab 4.0.0

Expand Down
2 changes: 1 addition & 1 deletion paket.lock
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ NUGET
Microsoft.EntityFrameworkCore (>= 8.0.2)
Microsoft.Extensions.Logging (>= 8.0)
xunit (>= 2.7)
Be.Vlaanderen.Basisregisters.Projector (15.0)
Be.Vlaanderen.Basisregisters.Projector (15.1)
Autofac (>= 8.0)
Autofac.Extensions.DependencyInjection (>= 9.0)
Be.Vlaanderen.Basisregisters.ProjectionHandling.Connector (>= 14.0)
Expand Down
55 changes: 55 additions & 0 deletions src/ParcelRegistry.Projections.BackOffice/HealthCheckRunner.cs
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);
}
}
}
}
18 changes: 17 additions & 1 deletion src/ParcelRegistry.Projections.BackOffice/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace ParcelRegistry.Projections.BackOffice
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using ParcelRegistry.Infrastructure;
Expand Down Expand Up @@ -86,7 +87,22 @@ public static async Task Main(string[] args)
.MigrationsHistoryTable(MigrationTables.BackOffice, Schema.BackOffice)
))
.AddHostedService<ProjectorRunner>()
.AddHostedService<ProjectionsHealthCheckRunner>();
.AddHostedService<HealthCheckRunner>();

foreach (var connectionString in hostContext.Configuration.GetSection("ConnectionStrings").GetChildren())
{
services.AddHealthChecks()
.AddSqlServer(
connectionString.Value,
name: $"sqlserver-{connectionString.Key.ToLowerInvariant()}",
tags: ["db", "sql", "sqlserver"]);
}

services.AddHealthChecks()
.AddDbContextCheck<BackOfficeContext>(
$"dbcontext-{nameof(BackOfficeContext).ToLowerInvariant()}",
tags: ["db", "sql", "sqlserver"])
.AddCheck<ProjectionsHealthCheck>("projections", failureStatus: HealthStatus.Unhealthy, tags: ["projections"]);
})
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureContainer<ContainerBuilder>((hostContext, builder) =>
Expand Down

This file was deleted.

2 changes: 2 additions & 0 deletions src/ParcelRegistry.Projections.BackOffice/paket.references
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Be.Vlaanderen.Basisregisters.ProjectionHandling.SqlStreamStore.Autofac
Be.Vlaanderen.Basisregisters.ProjectionHandling.Runner.SqlServer
Be.Vlaanderen.Basisregisters.Projector

AspNetCore.HealthChecks.SqlServer

Datadog.Trace.Bundle

SourceLink.Embed.AllSourceFiles
Expand Down

0 comments on commit 5b1bd1f

Please sign in to comment.