Skip to content

Commit bafd315

Browse files
committed
custom health check
1 parent 0014c70 commit bafd315

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

ShuttingDownHealthCheck.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
using Microsoft.Extensions.Diagnostics.HealthChecks;
5+
using Microsoft.Extensions.Hosting;
6+
7+
namespace blog_zero_downtime_with_health_checks
8+
{
9+
public class ShuttingDownHealthCheck : IHealthCheck
10+
{
11+
private HealthStatus _status = HealthStatus.Healthy;
12+
13+
public ShuttingDownHealthCheck(IHostApplicationLifetime appLifetime)
14+
{
15+
appLifetime.ApplicationStopping.Register(() =>
16+
{
17+
Console.WriteLine("Shutting down");
18+
_status = HealthStatus.Unhealthy;
19+
});
20+
}
21+
22+
public Task<HealthCheckResult> CheckHealthAsync(
23+
HealthCheckContext context,
24+
CancellationToken cancellationToken = default)
25+
{
26+
var result = new HealthCheckResult(_status);
27+
return Task.FromResult(result);
28+
}
29+
}
30+
}

Startup.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ public class Startup
1616
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
1717
public void ConfigureServices(IServiceCollection services)
1818
{
19-
services.AddHealthChecks();
19+
services.AddSingleton<ShuttingDownHealthCheck>();
20+
21+
services.AddHealthChecks()
22+
.AddCheck<ShuttingDownHealthCheck>("shutting_down");
2023
}
2124

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

0 commit comments

Comments
 (0)