0a85a839a2
Add ActiveNodeHealthCheck that returns 200 only on the Akka.NET cluster leader, enabling Traefik to route traffic to the active central node and automatically fail over when the leader changes. Also fixes AkkaClusterHealthCheck to resolve ActorSystem from AkkaHostedService (was always null via DI).
44 lines
1.4 KiB
C#
44 lines
1.4 KiB
C#
using Akka.Cluster;
|
|
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
|
using ScadaLink.Host.Actors;
|
|
|
|
namespace ScadaLink.Host.Health;
|
|
|
|
/// <summary>
|
|
/// Health check that verifies this node is an active member of the Akka.NET cluster.
|
|
/// Returns healthy only if the node's self-member status is Up or Joining.
|
|
/// </summary>
|
|
public class AkkaClusterHealthCheck : IHealthCheck
|
|
{
|
|
private readonly AkkaHostedService _akkaService;
|
|
|
|
public AkkaClusterHealthCheck(AkkaHostedService akkaService)
|
|
{
|
|
_akkaService = akkaService;
|
|
}
|
|
|
|
public Task<HealthCheckResult> CheckHealthAsync(
|
|
HealthCheckContext context,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
var system = _akkaService.ActorSystem;
|
|
if (system == null)
|
|
return Task.FromResult(HealthCheckResult.Degraded("ActorSystem not yet available."));
|
|
|
|
var cluster = Cluster.Get(system);
|
|
var status = cluster.SelfMember.Status;
|
|
|
|
var result = status switch
|
|
{
|
|
MemberStatus.Up or MemberStatus.Joining =>
|
|
HealthCheckResult.Healthy($"Akka cluster member status: {status}"),
|
|
MemberStatus.Leaving or MemberStatus.Exiting =>
|
|
HealthCheckResult.Degraded($"Akka cluster member status: {status}"),
|
|
_ =>
|
|
HealthCheckResult.Unhealthy($"Akka cluster member status: {status}")
|
|
};
|
|
|
|
return Task.FromResult(result);
|
|
}
|
|
}
|