Files
Umbraco-CMS/src/Umbraco.Web/Scheduling/HealthCheckNotifier.cs

86 lines
3.2 KiB
C#

using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Umbraco.Core;
using Umbraco.Core.Configuration.HealthChecks;
using Umbraco.Core.Logging;
using Umbraco.Web.HealthCheck;
namespace Umbraco.Web.Scheduling
{
internal class HealthCheckNotifier : RecurringTaskBase
{
private readonly ApplicationContext _appContext;
private readonly IHealthCheckResolver _healthCheckResolver;
public HealthCheckNotifier(IBackgroundTaskRunner<RecurringTaskBase> runner, int delayMilliseconds, int periodMilliseconds,
ApplicationContext appContext)
: base(runner, delayMilliseconds, periodMilliseconds)
{
_appContext = appContext;
_healthCheckResolver = HealthCheckResolver.Current;
}
public override async Task<bool> PerformRunAsync(CancellationToken token)
{
if (_appContext == null) return true; // repeat...
// ensure we do not run if not main domain, but do NOT lock it
if (_appContext.MainDom.IsMainDom == false)
{
LogHelper.Debug<HealthCheckNotifier>("Does not run if not MainDom.");
return false; // do NOT repeat, going down
}
using (DisposableTimer.DebugDuration<KeepAlive>(() => "Health checks executing", () => "Health checks complete"))
{
var healthCheckConfig = (HealthChecksSection)ConfigurationManager.GetSection("umbracoConfiguration/HealthChecks");
// 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 = _healthCheckResolver.HealthChecks
.Where(x => disabledCheckIds.Contains(x.Id) == false);
var sb = new StringBuilder();
foreach (var check in checks)
{
// TODO: get all sub-checks, not just first
var status = check.GetStatus().First();
sb.AppendFormat(" - Check {0} returned {1} with message {2}.", check.Name, status.ResultType, status.Message);
sb.AppendLine();
}
// TODO: get email address and send
if (!string.IsNullOrEmpty(healthCheckConfig.NotificationSettings.RecipientEmail))
{
}
// TODO: get web hook and post
if (!string.IsNullOrEmpty(healthCheckConfig.NotificationSettings.WebhookUrl))
{
}
LogHelper.Info<HealthCheckNotifier>("Health check results:");
LogHelper.Info<HealthCheckNotifier>(sb.ToString());
}
return true; // repeat
}
public override bool IsAsync
{
get { return true; }
}
}
}