Netcore: Health check notifier hosted service (#9295)

* Implemented health check notifier as a hosted service.
Added validation to health check settings.

* Registered health check notifier as a hosted service.
Modified health check nested settings to use concrete classes to align with other configuration models.

* Resolved issues with email sending using development server.

* PR review comments and fixed failing unit test.

* Changed period and delay millisecond and hourly values to TimeSpans.
Changed configuration of first run time for health check notifications to use H:mm format.

* Set up SecureSocketOptions as a locally defined enum.

* Tightened up time format validation to verify input is an actual time (with hours and minutes only) and not a timespan.

* Aligned naming and namespace of health check configuration related classes with other configuration classes.

* Created constants for hex colors used in formatting health check results as HTML.

* Revert "Tightened up time format validation to verify input is an actual time (with hours and minutes only) and not a timespan."

This reverts commit f9bb8a7a825bcb58146879f18b47922e09453e2d.

* Renamed method to be clear validation is of a TimeSpan and not a time.

Co-authored-by: Bjarke Berg <mail@bergmania.dk>
This commit is contained in:
Andy Butland
2020-10-30 13:56:13 +01:00
committed by GitHub
parent 4ae329589a
commit bdb8f34da3
31 changed files with 636 additions and 248 deletions

View File

@@ -1,110 +0,0 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Umbraco.Core;
using Umbraco.Core.Configuration.Models;
using Umbraco.Core.HealthCheck;
using Umbraco.Core.Logging;
using Umbraco.Core.Scoping;
using Umbraco.Core.Sync;
using Umbraco.Web.HealthCheck;
using Microsoft.Extensions.Logging;
using Umbraco.Infrastructure.HealthCheck;
namespace Umbraco.Web.Scheduling
{
public class HealthCheckNotifier : RecurringTaskBase
{
private readonly IMainDom _mainDom;
private readonly HealthCheckCollection _healthChecks;
private readonly HealthCheckNotificationMethodCollection _notifications;
private readonly IScopeProvider _scopeProvider;
private readonly IProfilingLogger _profilingLogger;
private readonly ILogger<HealthCheckNotifier> _logger;
private readonly HealthChecksSettings _healthChecksSettings;
private readonly IServerRegistrar _serverRegistrar;
private readonly IRuntimeState _runtimeState;
public HealthCheckNotifier(
IBackgroundTaskRunner<RecurringTaskBase> runner,
int delayMilliseconds,
int periodMilliseconds,
HealthCheckCollection healthChecks,
HealthCheckNotificationMethodCollection notifications,
IMainDom mainDom,
IProfilingLogger profilingLogger ,
ILogger<HealthCheckNotifier> logger,
HealthChecksSettings healthChecksSettings,
IServerRegistrar serverRegistrar,
IRuntimeState runtimeState,
IScopeProvider scopeProvider)
: base(runner, delayMilliseconds, periodMilliseconds)
{
_healthChecks = healthChecks;
_notifications = notifications;
_mainDom = mainDom;
_scopeProvider = scopeProvider;
_runtimeState = runtimeState;
_profilingLogger = profilingLogger ;
_logger = logger;
_healthChecksSettings = healthChecksSettings;
_serverRegistrar = serverRegistrar;
_runtimeState = runtimeState;
}
public override async Task<bool> PerformRunAsync(CancellationToken token)
{
if (_runtimeState.Level != RuntimeLevel.Run)
return true; // repeat...
switch (_serverRegistrar.GetCurrentServerRole())
{
case ServerRole.Replica:
_logger.LogDebug("Does not run on replica servers.");
return true; // DO repeat, server role can change
case ServerRole.Unknown:
_logger.LogDebug("Does not run on servers with unknown role.");
return true; // DO repeat, server role can change
}
// ensure we do not run if not main domain, but do NOT lock it
if (_mainDom.IsMainDom == false)
{
_logger.LogDebug("Does not run if not MainDom.");
return false; // do NOT repeat, going down
}
// Ensure we use an explicit scope since we are running on a background thread and plugin health
// checks can be making service/database calls so we want to ensure the CallContext/Ambient scope
// isn't used since that can be problematic.
using (var scope = _scopeProvider.CreateScope())
using (_profilingLogger.DebugDuration<HealthCheckNotifier>("Health checks executing", "Health checks complete"))
{
var healthCheckConfig = _healthChecksSettings;
// Don't notify for any checks that are disabled, nor for any disabled
// just for notifications
var disabledCheckIds = healthCheckConfig.NotificationSettings.DisabledChecks
.Select(x => x.Id)
.Union(healthCheckConfig.DisabledChecks
.Select(x => x.Id))
.Distinct()
.ToArray();
var checks = _healthChecks
.Where(x => disabledCheckIds.Contains(x.Id) == false);
var results = new HealthCheckResults(checks);
results.LogResults();
// Send using registered notification methods that are enabled
foreach (var notificationMethod in _notifications.Where(x => x.Enabled))
await notificationMethod.SendAsync(results, token);
}
return true; // repeat
}
public override bool IsAsync => true;
}
}

View File

@@ -36,10 +36,7 @@ namespace Umbraco.Web.Scheduling
private readonly ILoggerFactory _loggerFactory;
private readonly IApplicationShutdownRegistry _applicationShutdownRegistry;
private readonly IScopeProvider _scopeProvider;
private readonly HealthCheckCollection _healthChecks;
private readonly HealthCheckNotificationMethodCollection _notifications;
private readonly IUmbracoContextFactory _umbracoContextFactory;
private readonly HealthChecksSettings _healthChecksSettings;
private readonly IServerMessenger _serverMessenger;
private readonly IRequestAccessor _requestAccessor;
private readonly IBackofficeSecurityFactory _backofficeSecurityFactory;
@@ -59,9 +56,8 @@ namespace Umbraco.Web.Scheduling
public SchedulerComponent(IRuntimeState runtime, IMainDom mainDom, IServerRegistrar serverRegistrar,
IContentService contentService, IAuditService auditService,
HealthCheckCollection healthChecks, HealthCheckNotificationMethodCollection notifications,
IScopeProvider scopeProvider, IUmbracoContextFactory umbracoContextFactory, IProfilingLogger profilingLogger , ILoggerFactory loggerFactory,
IApplicationShutdownRegistry applicationShutdownRegistry, IOptions<HealthChecksSettings> healthChecksSettings,
IScopeProvider scopeProvider, IUmbracoContextFactory umbracoContextFactory, IProfilingLogger profilingLogger, ILoggerFactory loggerFactory,
IApplicationShutdownRegistry applicationShutdownRegistry,
IServerMessenger serverMessenger, IRequestAccessor requestAccessor,
IOptions<LoggingSettings> loggingSettings, IOptions<KeepAliveSettings> keepAliveSettings,
IHostingEnvironment hostingEnvironment,
@@ -73,15 +69,11 @@ namespace Umbraco.Web.Scheduling
_contentService = contentService;
_auditService = auditService;
_scopeProvider = scopeProvider;
_profilingLogger = profilingLogger ;
_profilingLogger = profilingLogger;
_loggerFactory = loggerFactory;
_logger = loggerFactory.CreateLogger<SchedulerComponent>();
_applicationShutdownRegistry = applicationShutdownRegistry;
_umbracoContextFactory = umbracoContextFactory;
_healthChecks = healthChecks;
_notifications = notifications;
_healthChecksSettings = healthChecksSettings.Value ?? throw new ArgumentNullException(nameof(healthChecksSettings));
_serverMessenger = serverMessenger;
_requestAccessor = requestAccessor;
_backofficeSecurityFactory = backofficeSecurityFactory;
@@ -98,7 +90,6 @@ namespace Umbraco.Web.Scheduling
_publishingRunner = new BackgroundTaskRunner<IBackgroundTask>("ScheduledPublishing", logger, _applicationShutdownRegistry);
_scrubberRunner = new BackgroundTaskRunner<IBackgroundTask>("LogScrubber", logger, _applicationShutdownRegistry);
_fileCleanupRunner = new BackgroundTaskRunner<IBackgroundTask>("TempFileCleanup", logger, _applicationShutdownRegistry);
_healthCheckRunner = new BackgroundTaskRunner<IBackgroundTask>("HealthCheckNotifier", logger, _applicationShutdownRegistry);
// we will start the whole process when a successful request is made
_requestAccessor.RouteAttempt += RegisterBackgroundTasksOnce;
@@ -138,10 +129,6 @@ namespace Umbraco.Web.Scheduling
tasks.Add(RegisterLogScrubber(_loggingSettings));
tasks.Add(RegisterTempFileCleanup());
var healthCheckConfig = _healthChecksSettings;
if (healthCheckConfig.NotificationSettings.Enabled)
tasks.Add(RegisterHealthCheckNotifier(healthCheckConfig, _healthChecks, _notifications, _profilingLogger));
return tasks.ToArray();
});
}
@@ -164,32 +151,6 @@ namespace Umbraco.Web.Scheduling
return task;
}
private IBackgroundTask RegisterHealthCheckNotifier(HealthChecksSettings healthCheckSettingsConfig,
HealthCheckCollection healthChecks, HealthCheckNotificationMethodCollection notifications,
IProfilingLogger logger)
{
// If first run time not set, start with just small delay after application start
int delayInMilliseconds;
if (string.IsNullOrEmpty(healthCheckSettingsConfig.NotificationSettings.FirstRunTime))
{
delayInMilliseconds = DefaultDelayMilliseconds;
}
else
{
// Otherwise start at scheduled time
delayInMilliseconds = DateTime.Now.PeriodicMinutesFrom(healthCheckSettingsConfig.NotificationSettings.FirstRunTime) * 60 * 1000;
if (delayInMilliseconds < DefaultDelayMilliseconds)
{
delayInMilliseconds = DefaultDelayMilliseconds;
}
}
var periodInMilliseconds = healthCheckSettingsConfig.NotificationSettings.PeriodInHours * 60 * 60 * 1000;
var task = new HealthCheckNotifier(_healthCheckRunner, delayInMilliseconds, periodInMilliseconds, healthChecks, notifications, _mainDom, logger, _loggerFactory.CreateLogger<HealthCheckNotifier>(), _healthChecksSettings, _serverRegistrar, _runtime, _scopeProvider);
_healthCheckRunner.TryAdd(task);
return task;
}
private IBackgroundTask RegisterLogScrubber(LoggingSettings settings)
{
// log scrubbing