Last active
November 14, 2019 01:33
-
-
Save dcomartin/e17ba8d2ec9ba1297909f31aa11e8218 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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