Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.