From bf194a819a0f8cb158379f621773d6cda7fced75 Mon Sep 17 00:00:00 2001 From: AndyButland Date: Mon, 5 Jun 2017 13:35:08 +0200 Subject: [PATCH] Added try/catch around scheduled checks so one failure doesn't block the rest. Applied some formatting to email HTML notification. --- .../HealthCheck/HealthCheckResults.cs | 44 +++++++++++++++++-- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/src/Umbraco.Web/HealthCheck/HealthCheckResults.cs b/src/Umbraco.Web/HealthCheck/HealthCheckResults.cs index 1a48ee1b5e..1781a0a423 100644 --- a/src/Umbraco.Web/HealthCheck/HealthCheckResults.cs +++ b/src/Umbraco.Web/HealthCheck/HealthCheckResults.cs @@ -13,7 +13,26 @@ namespace Umbraco.Web.HealthCheck internal HealthCheckResults(IEnumerable checks) { - _results = checks.ToDictionary(t => t.Name, t => t.GetStatus()); + _results = checks.ToDictionary( + t => t.Name, + t => { + try + { + return t.GetStatus(); + } + catch (Exception ex) + { + LogHelper.Error(string.Format("Error running scheduled health check: {0}", t.Name), ex); + var message = string.Format("Health check failed with exception: {0}. See logs for details.", ex.Message); + return new List + { + new HealthCheckStatus(message) + { + ResultType = StatusResultType.Error + } + }; + } + }); } internal void LogResults() @@ -66,17 +85,34 @@ namespace Umbraco.Web.HealthCheck foreach (var checkResult in checkResults) { - sb.AppendFormat("\t{0}Result: '{1}' , Message: '{2}'{3}", newItem, checkResult.ResultType, SimpleHtmlToMarkDown(checkResult.Message, slackMarkDown), Environment.NewLine); + sb.AppendFormat("\t{0}Result: '{1}', Message: '{2}'{3}", newItem, checkResult.ResultType, SimpleHtmlToMarkDown(checkResult.Message, slackMarkDown), Environment.NewLine); } } + return sb.ToString(); } internal string ResultsAsHtml() { - Markdown mark = new Markdown(); - return mark.Transform(ResultsAsMarkDown()); + var mark = new Markdown(); + var html = mark.Transform(ResultsAsMarkDown()); + html = ApplyHtmlHighlighting(html); + return html; } + + private string ApplyHtmlHighlighting(string html) + { + html = ApplyHtmlHighlightingForStatus(html, StatusResultType.Success, "5cb85c"); + html = ApplyHtmlHighlightingForStatus(html, StatusResultType.Warning, "f0ad4e"); + return ApplyHtmlHighlightingForStatus(html, StatusResultType.Error, "d9534f"); + } + + private string ApplyHtmlHighlightingForStatus(string html, StatusResultType status, string color) + { + return html + .Replace("Result: '" + status + "'", "Result " + status + ""); + } + private string SimpleHtmlToMarkDown(string html, bool slackMarkDown = false) { if (slackMarkDown)