Skip to content
Permalink
Browse files
custom health check
  • Loading branch information
cwe1ss committed Oct 22, 2020
1 parent 0014c70 commit bafd315f72a4555074db2bb4a4e5f15c8c1f0a9b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
@@ -0,0 +1,30 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Hosting;

namespace blog_zero_downtime_with_health_checks
{
public class ShuttingDownHealthCheck : IHealthCheck
{
private HealthStatus _status = HealthStatus.Healthy;

public ShuttingDownHealthCheck(IHostApplicationLifetime appLifetime)
{
appLifetime.ApplicationStopping.Register(() =>
{
Console.WriteLine("Shutting down");
_status = HealthStatus.Unhealthy;
});
}

public Task<HealthCheckResult> CheckHealthAsync(
HealthCheckContext context,
CancellationToken cancellationToken = default)
{
var result = new HealthCheckResult(_status);
return Task.FromResult(result);
}
}
}
@@ -16,7 +16,10 @@ public class Startup
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddHealthChecks();
services.AddSingleton<ShuttingDownHealthCheck>();

services.AddHealthChecks()
.AddCheck<ShuttingDownHealthCheck>("shutting_down");
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

0 comments on commit bafd315

Please sign in to comment.