Xabaril / AspNetCore.Diagnostics.HealthChecks

Enterprise HealthChecks for ASP.NET Core Diagnostics Package
Apache License 2.0
4.08k stars 797 forks source link

Is there a way to restrict when the Health Checks run? #2094

Open Genide opened 11 months ago

Genide commented 11 months ago

What would you like to be added: I want to only run these HealthChecks around business hours. So ideally, given the specific HealthCheck information and current time, I want to be able to specify whether the HealthCheck collector will run. This would in conjunction with "SetEvaluationTimeInSeconds".

This was mentioned in a previous issue #459, but no action was taken.

Why is this needed: I need this to run at only specific times because the healthchecks cause pinged sites to stay up forever without hibernating. I am currently pinging sites on IIS and they will never go to sleep with the healthcheck pinging so frequently.

Example Setup Code

services
.AddHealthChecksUI(setupSettings: setup =>
{     
    setup.ShouldExecuteHealthChecksCollectorCallback
        (serviceProvider=> DateTime.UtcNow.Hour >= 8 && DateTime.UtcNow.Hour < 20);
});

Or... another dummy sample:

services
 .AddHealthChecksUI(setupSettings: setup =>
{
 setup.ShouldExecuteHealthChecksCollectorCallback(serviceProvider=> {
    var scheduler = serviceProvider.GetRequiredService<IHealthExecutorScheduler>();
    return scheduler.ShouldExecute();
});
sungam3r commented 10 months ago

PR is welcome.