Skip to content

Commit

Permalink
custom health check
Browse files Browse the repository at this point in the history
  • Loading branch information
cwe1ss committed Oct 22, 2020
1 parent 0014c70 commit bafd315
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
30 changes: 30 additions & 0 deletions ShuttingDownHealthCheck.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
5 changes: 4 additions & 1 deletion Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit bafd315

Please sign in to comment.