/// <summary> /// Initializes a new instance of the <see cref="HealthCheckNotifier"/> class. /// </summary> /// <param name="healthChecksSettings">The configuration for health check settings.</param> /// <param name="healthChecks">The collection of healthchecks.</param> /// <param name="notifications">The collection of healthcheck notification methods.</param> /// <param name="runtimeState">Representation of the state of the Umbraco runtime.</param> /// <param name="serverRegistrar">Provider of server registrations to the distributed cache.</param> /// <param name="mainDom">Representation of the main application domain.</param> /// <param name="scopeProvider">Provides scopes for database operations.</param> /// <param name="logger">The typed logger.</param> /// <param name="profilingLogger">The profiling logger.</param> /// <param name="cronTabParser">Parser of crontab expressions.</param> public HealthCheckNotifier( IOptionsMonitor <HealthChecksSettings> healthChecksSettings, HealthCheckCollection healthChecks, HealthCheckNotificationMethodCollection notifications, IRuntimeState runtimeState, IServerRoleAccessor serverRegistrar, IMainDom mainDom, ICoreScopeProvider scopeProvider, ILogger <HealthCheckNotifier> logger, IProfilingLogger profilingLogger, ICronTabParser cronTabParser) : base( logger, healthChecksSettings.CurrentValue.Notification.Period, healthChecksSettings.CurrentValue.GetNotificationDelay(cronTabParser, DateTime.Now, DefaultDelay)) { _healthChecksSettings = healthChecksSettings.CurrentValue; _healthChecks = healthChecks; _notifications = notifications; _runtimeState = runtimeState; _serverRegistrar = serverRegistrar; _mainDom = mainDom; _scopeProvider = scopeProvider; _logger = logger; _profilingLogger = profilingLogger; healthChecksSettings.OnChange(x => { _healthChecksSettings = x; ChangePeriod(x.Notification.Period); }); }
public void Returns_Fail_For_Configuration_With_Invalid_Notification_FirstRunTime() { var validator = new HealthChecksSettingsValidator(new NCronTabParser()); HealthChecksSettings options = BuildHealthChecksSettings(firstRunTime: "0 3 *"); ValidateOptionsResult result = validator.Validate("settings", options); Assert.False(result.Succeeded); }
public void Returns_Success_ForValid_Configuration() { var validator = new HealthChecksSettingsValidator(new NCronTabParser()); HealthChecksSettings options = BuildHealthChecksSettings(); ValidateOptionsResult result = validator.Validate("settings", options); Assert.True(result.Succeeded); }
/// <summary> /// Initializes a new instance of the <see cref="HealthCheckController"/> class. /// </summary> public HealthCheckController(HealthCheckCollection checks, ILogger <HealthCheckController> logger, IOptions <HealthChecksSettings> healthChecksSettings) { _checks = checks ?? throw new ArgumentNullException(nameof(checks)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); HealthChecksSettings healthCheckConfig = healthChecksSettings.Value ?? throw new ArgumentNullException(nameof(healthChecksSettings)); _disabledCheckIds = healthCheckConfig.DisabledChecks .Select(x => x.Id) .ToList(); }
public void Returns_Notification_Delay_From_Default_When_Provided_Time_Too_Close_To_Current_Time() { var settings = new HealthChecksSettings { Notification = new HealthChecksNotificationSettings { FirstRunTime = "30 12 * * *" }, }; var now = new DateTime(2020, 10, 31, 12, 25, 0); var result = settings.GetNotificationDelay(CronTabParser, now, TimeSpan.FromMinutes(10)); Assert.AreEqual(10, result.TotalMinutes); }
public void Returns_Notification_Delay_From_Provided_Time(string firstRunTimeCronExpression, int expectedDelayInMinutes) { var settings = new HealthChecksSettings { Notification = new HealthChecksNotificationSettings { FirstRunTime = firstRunTimeCronExpression }, }; var now = new DateTime(2020, 10, 31, 12, 0, 0); var result = settings.GetNotificationDelay(CronTabParser, now, TimeSpan.Zero); Assert.AreEqual(expectedDelayInMinutes, result.TotalMinutes); }
public static TimeSpan GetNotificationDelay(this HealthChecksSettings settings, ICronTabParser cronTabParser, DateTime now, TimeSpan defaultDelay) { // If first run time not set, start with just small delay after application start. var firstRunTime = settings.Notification.FirstRunTime; if (string.IsNullOrEmpty(firstRunTime)) { return(defaultDelay); } // Otherwise start at scheduled time according to cron expression, unless within the default delay period. DateTime firstRunOccurance = cronTabParser.GetNextOccurrence(firstRunTime, now); TimeSpan delay = firstRunOccurance - now; return(delay < defaultDelay ? defaultDelay : delay); }
private HealthCheckNotifier CreateHealthCheckNotifier( bool enabled = true, RuntimeLevel runtimeLevel = RuntimeLevel.Run, ServerRole serverRole = ServerRole.Single, bool isMainDom = true, bool notificationEnabled = true) { var settings = new HealthChecksSettings { Notification = new HealthChecksNotificationSettings { Enabled = enabled, DisabledChecks = new List <DisabledHealthCheckSettings> { new DisabledHealthCheckSettings { Id = Guid.Parse(Check3Id) } } }, DisabledChecks = new List <DisabledHealthCheckSettings> { new DisabledHealthCheckSettings { Id = Guid.Parse(Check2Id) } } }; var checks = new HealthCheckCollection(() => new List <HealthCheck> { new TestHealthCheck1(), new TestHealthCheck2(), new TestHealthCheck3(), }); _mockNotificationMethod = new Mock <IHealthCheckNotificationMethod>(); _mockNotificationMethod.SetupGet(x => x.Enabled).Returns(notificationEnabled); var notifications = new HealthCheckNotificationMethodCollection(() => new List <IHealthCheckNotificationMethod> { _mockNotificationMethod.Object }); var mockRunTimeState = new Mock <IRuntimeState>(); mockRunTimeState.SetupGet(x => x.Level).Returns(runtimeLevel); var mockServerRegistrar = new Mock <IServerRoleAccessor>(); mockServerRegistrar.Setup(x => x.CurrentServerRole).Returns(serverRole); var mockMainDom = new Mock <IMainDom>(); mockMainDom.SetupGet(x => x.IsMainDom).Returns(isMainDom); var mockScopeProvider = new Mock <IScopeProvider>(); var mockLogger = new Mock <ILogger <HealthCheckNotifier> >(); var mockProfilingLogger = new Mock <IProfilingLogger>(); return(new HealthCheckNotifier( Options.Create(settings), checks, notifications, mockRunTimeState.Object, mockServerRegistrar.Object, mockMainDom.Object, mockScopeProvider.Object, mockLogger.Object, mockProfilingLogger.Object, Mock.Of <ICronTabParser>())); }