AspNetCore.Diagnostics.HealthChecks icon indicating copy to clipboard operation
AspNetCore.Diagnostics.HealthChecks copied to clipboard

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

Open Genide opened this issue 1 year ago • 1 comments

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();
});

Genide avatar Nov 17 '23 20:11 Genide

PR is welcome.

sungam3r avatar Dec 04 '23 06:12 sungam3r