Ejemplo 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);
                    }
                }
        }
Ejemplo n.º 2
0
        public override async Task <bool> PerformRunAsync(CancellationToken token)
        {
            if (_runtimeState.Level != RuntimeLevel.Run)
            {
                return(true); // repeat...
            }
            switch (_runtimeState.ServerRole)
            {
            case ServerRole.Replica:
                _logger.Debug <HealthCheckNotifier>("Does not run on replica servers.");
                return(true);    // DO repeat, server role can change

            case ServerRole.Unknown:
                _logger.Debug <HealthCheckNotifier>("Does not run on servers with unknown role.");
                return(true);    // DO repeat, server role can change
            }

            // ensure we do not run if not main domain, but do NOT lock it
            if (_runtimeState.IsMainDom == false)
            {
                _logger.Debug <HealthCheckNotifier>("Does not run if not MainDom.");
                return(false); // do NOT repeat, going down
            }

            // 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 (var scope = _scopeProvider.CreateScope())
                using (_logger.DebugDuration <HealthCheckNotifier>("Health checks executing", "Health checks complete"))
                {
                    var healthCheckConfig = Current.Configs.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 = _healthChecks
                                 .Where(x => disabledCheckIds.Contains(x.Id) == false);

                    var results = new HealthCheckResults(checks);
                    results.LogResults();

                    // Send using registered notification methods that are enabled
                    foreach (var notificationMethod in _notifications.Where(x => x.Enabled))
                    {
                        await notificationMethod.SendAsync(results, token);
                    }
                }

            return(true); // repeat
        }
        public override async Task <bool> PerformRunAsync(CancellationToken token)
        {
            if (_runtimeState.Level != RuntimeLevel.Run)
            {
                return(true); // repeat...
            }
            switch (_runtimeState.ServerRole)
            {
            case ServerRole.Replica:
                _logger.Debug <HealthCheckNotifier>("Does not run on replica servers.");
                return(true);    // DO repeat, server role can change

            case ServerRole.Unknown:
                _logger.Debug <HealthCheckNotifier>("Does not run on servers with unknown role.");
                return(true);    // DO repeat, server role can change
            }

            // ensure we do not run if not main domain, but do NOT lock it
            if (_runtimeState.IsMainDom == false)
            {
                _logger.Debug <HealthCheckNotifier>("Does not run if not MainDom.");
                return(false); // do NOT repeat, going down
            }

            using (_logger.DebugDuration <HealthCheckNotifier>("Health checks executing", "Health checks complete"))
            {
                var healthCheckConfig = Current.Configs.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 = _healthChecks
                             .Where(x => disabledCheckIds.Contains(x.Id) == false);

                var results = new HealthCheckResults(checks);
                results.LogResults();

                // Send using registered notification methods that are enabled
                foreach (var notificationMethod in _notifications.Where(x => x.Enabled))
                {
                    await notificationMethod.SendAsync(results, token);
                }
            }

            return(true); // repeat
        }