Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Last active November 14, 2019 01:33
Show Gist options
  • Save dcomartin/e17ba8d2ec9ba1297909f31aa11e8218 to your computer and use it in GitHub Desktop.
Save dcomartin/e17ba8d2ec9ba1297909f31aa11e8218 to your computer and use it in GitHub Desktop.
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Orleans.AspNetCore.Demo.Grains;
namespace Orleans.AspNetCore.Demo
{
public class OrleansHealthCheck : IHealthCheck
{
private readonly IClusterClient _clusterClient;
public OrleansHealthCheck(IClusterClient clusterClient)
{
_clusterClient = clusterClient;
}
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
if (_clusterClient.IsInitialized == false)
{
return HealthCheckResult.Healthy("Initializing");
}
try
{
return await _clusterClient.GetGrain<IHealthCheckGrain>(Guid.Empty).IsHealthy()
? HealthCheckResult.Healthy()
: HealthCheckResult.Unhealthy();
}
catch (Exception ex)
{
return HealthCheckResult.Unhealthy("HealthCheckGrain failed.", ex);
}
}
}
public static class OrleansHealthCheckBuilderExtension
{
private const string HealthCheckName = "OrleansHealthCheck";
public static IHealthChecksBuilder AddOrleansHealthCheck(this IHealthChecksBuilder builder)
{
return builder.Add(new HealthCheckRegistration(HealthCheckName, sp => new OrleansHealthCheck( sp.GetRequiredService<IClusterClient>()), HealthStatus.Unhealthy, new string[0]));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment