Esempio n. 1
0
        public override async Task PerformExecuteAsync(object?state)
        {
            if (_healthChecksSettings.Notification.Enabled == false)
            {
                return;
            }

            if (_runtimeState.Level != RuntimeLevel.Run)
            {
                return;
            }

            switch (_serverRegistrar.CurrentServerRole)
            {
            case ServerRole.Subscriber:
                _logger.LogDebug("Does not run on subscriber servers.");
                return;

            case ServerRole.Unknown:
                _logger.LogDebug("Does not run on servers with unknown role.");
                return;
            }

            // 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;
            }

            // 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 (ICoreScope scope = _scopeProvider.CreateCoreScope())
                using (_profilingLogger.DebugDuration <HealthCheckNotifier>("Health checks executing", "Health checks complete"))
                {
                    // Don't notify for any checks that are disabled, nor for any disabled just for notifications.
                    Guid[] disabledCheckIds = _healthChecksSettings.Notification.DisabledChecks
                                              .Select(x => x.Id)
                                              .Union(_healthChecksSettings.DisabledChecks
                                                     .Select(x => x.Id))
                                              .Distinct()
                                              .ToArray();

                    IEnumerable <HealthCheck> checks = _healthChecks
                                                       .Where(x => disabledCheckIds.Contains(x.Id) == false);

                    var results = await HealthCheckResults.Create(checks);

                    results.LogResults();

                    // Send using registered notification methods that are enabled.
                    foreach (IHealthCheckNotificationMethod notificationMethod in _notifications.Where(x => x.Enabled))
                    {
                        await notificationMethod.SendAsync(results);
                    }
                }
        }
        public async Task HealthCheckResults_WithSummaryVerbosity_ReturnsCorrectResultDescription()
        {
            var checks = new List <HealthCheck>
            {
                new StubHealthCheck1(StatusResultType.Success, "First check was successful"),
                new StubHealthCheck2(StatusResultType.Success, "Second check was successful"),
            };
            var results = await HealthCheckResults.Create(checks);

            var resultAsMarkdown = results.ResultsAsMarkDown(HealthCheckNotificationVerbosity.Summary);

            Assert.IsTrue(resultAsMarkdown.IndexOf("Result: 'Success'" + Environment.NewLine) > -1);
        }
        public async Task HealthCheckResults_WithFailingChecks_ReturnsCorrectResultDescription()
        {
            var checks = new List <HealthCheck>
            {
                new StubHealthCheck1(StatusResultType.Success, "First check was successful"),
                new StubHealthCheck2(StatusResultType.Error, "Second check was not successful"),
            };
            var results = await HealthCheckResults.Create(checks);

            Assert.IsFalse(results.AllChecksSuccessful);

            var resultAsMarkdown = results.ResultsAsMarkDown(HealthCheckNotificationVerbosity.Summary);

            Assert.IsTrue(resultAsMarkdown.IndexOf("Checks for 'Stub check 1' all completed successfully.") > -1);
            Assert.IsTrue(resultAsMarkdown.IndexOf("Checks for 'Stub check 2' completed with errors.") > -1);
        }