Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(backoffice): projections healthcheck #1275

Merged
merged 2 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion paket.dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,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 @@ -379,7 +379,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
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
namespace BuildingRegistry.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);

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);

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/BuildingRegistry.Projections.BackOffice/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace BuildingRegistry.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 Serilog;
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.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Be.Vlaanderen.Basisregisters.ProjectionHandling.Runner.SqlServer
Be.Vlaanderen.Basisregisters.ProjectionHandling.SqlStreamStore.Autofac
Be.Vlaanderen.Basisregisters.Projector

AspNetCore.HealthChecks.SqlServer

Datadog.Trace.Bundle

SourceLink.Embed.AllSourceFiles
Expand Down
Loading