Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HealthCheckCompletedNotification #15276

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Umbraco.Core/HealthChecks/HealthCheckResults.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Text;

Check warning on line 1 in src/Umbraco.Core/HealthChecks/HealthCheckResults.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v13/dev)

❌ New issue: Overall Code Complexity

This module has a mean cyclomatic complexity of 4.43 across 7 functions. The mean complexity threshold is 4. This file has many conditional statements (e.g. if, for, while) across its implementation, leading to lower code health. Avoid adding more conditionals.
using Microsoft.Extensions.Logging;
using Umbraco.Extensions;

Expand Down Expand Up @@ -53,6 +53,8 @@
return new HealthCheckResults(results, allChecksSuccessful);
}

public static async Task<HealthCheckResults> Create(HealthCheck check) => await Create(new List<HealthCheck>() { check });

public void LogResults()
{
Logger.LogInformation("Scheduled health check results:");
Expand Down
13 changes: 13 additions & 0 deletions src/Umbraco.Core/Notifications/HealthCheckCompletedNotification.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Umbraco.Cms.Core.HealthChecks;

namespace Umbraco.Cms.Core.Notifications;

public class HealthCheckCompletedNotification : INotification
{
public HealthCheckCompletedNotification(HealthCheckResults healthCheckResults)
{
HealthCheckResults = healthCheckResults;
}

public HealthCheckResults HealthCheckResults { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Configuration;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.HealthChecks;
using Umbraco.Cms.Core.HealthChecks.NotificationMethods;
using Umbraco.Cms.Core.Logging;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Core.Runtime;
using Umbraco.Cms.Core.Scoping;
using Umbraco.Cms.Core.Services;
Expand Down Expand Up @@ -38,6 +40,7 @@
private readonly ILogger<HealthCheckNotifierJob> _logger;
private readonly HealthCheckNotificationMethodCollection _notifications;
private readonly IProfilingLogger _profilingLogger;
private readonly IEventAggregator _eventAggregator;
private readonly ICoreScopeProvider _scopeProvider;
private HealthChecksSettings _healthChecksSettings;

Expand All @@ -58,14 +61,16 @@
ICoreScopeProvider scopeProvider,
ILogger<HealthCheckNotifierJob> logger,
IProfilingLogger profilingLogger,
ICronTabParser cronTabParser)
ICronTabParser cronTabParser,
IEventAggregator eventAggregator)
{
_healthChecksSettings = healthChecksSettings.CurrentValue;
_healthChecks = healthChecks;
_notifications = notifications;
_scopeProvider = scopeProvider;
_logger = logger;
_profilingLogger = profilingLogger;
_eventAggregator = eventAggregator;

Check notice on line 73 in src/Umbraco.Infrastructure/BackgroundJobs/Jobs/HealthCheckNotifierJob.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v13/dev)

ℹ Getting worse: Constructor Over-Injection

HealthCheckNotifierJob increases from 7 to 8 arguments, threshold = 5. This constructor has too many arguments, indicating an object with low cohesion or missing function argument abstraction. Avoid adding more arguments.

Period = healthChecksSettings.CurrentValue.Notification.Period;
Delay = DelayCalculator.GetDelay(healthChecksSettings.CurrentValue.Notification.FirstRunTime, cronTabParser, logger, TimeSpan.FromMinutes(3));
Expand Down Expand Up @@ -106,6 +111,8 @@
HealthCheckResults results = await HealthCheckResults.Create(checks);
results.LogResults();

_eventAggregator.Publish(new HealthCheckCompletedNotification(results));

// Send using registered notification methods that are enabled.
foreach (IHealthCheckNotificationMethod notificationMethod in _notifications.Where(x => x.Enabled))
{
Expand Down
18 changes: 17 additions & 1 deletion src/Umbraco.Web.BackOffice/HealthChecks/HealthCheckController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
using Microsoft.Extensions.Options;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.HealthChecks;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Web.BackOffice.Controllers;
using Umbraco.Cms.Web.Common.Attributes;
using Umbraco.Cms.Web.Common.Authorization;
Expand All @@ -24,14 +26,18 @@ public class HealthCheckController : UmbracoAuthorizedJsonController
private readonly HealthCheckCollection _checks;
private readonly IList<Guid> _disabledCheckIds;
private readonly ILogger<HealthCheckController> _logger;
private readonly IEventAggregator _eventAggregator;
private readonly HealthChecksSettings _healthChecksSettings;

/// <summary>
/// Initializes a new instance of the <see cref="HealthCheckController" /> class.
/// </summary>
public HealthCheckController(HealthCheckCollection checks, ILogger<HealthCheckController> logger, IOptions<HealthChecksSettings> healthChecksSettings)
public HealthCheckController(HealthCheckCollection checks, ILogger<HealthCheckController> logger, IOptions<HealthChecksSettings> healthChecksSettings, IEventAggregator eventAggregator)
erikjanwestendorp marked this conversation as resolved.
Show resolved Hide resolved
{
_checks = checks ?? throw new ArgumentNullException(nameof(checks));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_eventAggregator = eventAggregator ?? throw new ArgumentException(nameof(eventAggregator));
_healthChecksSettings = healthChecksSettings?.Value ?? throw new ArgumentException(nameof(healthChecksSettings));

HealthChecksSettings healthCheckConfig =
healthChecksSettings.Value ?? throw new ArgumentNullException(nameof(healthChecksSettings));
Expand Down Expand Up @@ -80,6 +86,16 @@ public async Task<object> GetStatus(Guid id)
{
_logger.LogDebug("Running health check: " + check.Name);
}

if (!_healthChecksSettings.Notification.Enabled)
{
return await check.GetStatus();
}

HealthCheckResults results = await HealthCheckResults.Create(check);
_eventAggregator.Publish(new HealthCheckCompletedNotification(results));


return await check.GetStatus();
}
catch (Exception ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Configuration;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.HealthChecks;
using Umbraco.Cms.Core.HealthChecks.NotificationMethods;
using Umbraco.Cms.Core.Logging;
Expand Down Expand Up @@ -105,7 +106,8 @@ private HealthCheckNotifierJob CreateHealthCheckNotifier(
mockScopeProvider.Object,
mockLogger.Object,
mockProfilingLogger.Object,
Mock.Of<ICronTabParser>());
Mock.Of<ICronTabParser>(),
Mock.Of<IEventAggregator>());
}

private void VerifyNotificationsNotSent() => VerifyNotificationsSentTimes(Times.Never());
Expand Down
Loading