Example #1
0
        public Task <HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
        {
            IHealthCheckService healthCheckService = Application.Resolve <IHealthCheckService>();

            IEnumerable <Core.IHealthCheck>      checks = healthCheckService.GetHealthChecks();
            IReadOnlyDictionary <string, object> dic    = checks.ToDictionary(x => x.Name, v => (object)v);

            if (checks.Any(x => x.Failed))
            {
                return(Task.FromResult(
                           HealthCheckResult.Degraded(
                               "WARNING",
                               data: dic)));
            }

            return(Task.FromResult(
                       HealthCheckResult.Healthy(
                           "OK",
                           data: dic)));
        }
Example #2
0
        [HttpGet("status")] // TODO
        public StatusApiModel Status()
        {
            ILoggingService     loggingService     = Application.Resolve <ILoggingService>();
            ITradingService     tradingService     = Application.Resolve <ITradingService>();
            ISignalsService     signalsService     = Application.Resolve <ISignalsService>();
            IHealthCheckService healthCheckService = Application.Resolve <IHealthCheckService>();

            var status = new StatusApiModel
            {
                Balance          = tradingService.Account.GetBalance(),
                GlobalRating     = signalsService.GetGlobalRating()?.ToString("0.000") ?? "N/A",
                TrailingBuys     = tradingService.GetTrailingBuys(),
                TrailingSells    = tradingService.GetTrailingSells(),
                TrailingSignals  = signalsService.GetTrailingSignals(),
                TradingSuspended = tradingService.IsTradingSuspended,
                HealthChecks     = healthCheckService.GetHealthChecks().OrderBy(c => c.Name),
                LogEntries       = loggingService.GetLogEntries().Reverse().Take(5)
            };

            return(status);
        }
Example #3
0
 public PartialViewResult List()
 {
     return(PartialView(_healthCheckService.GetHealthChecks()));
 }
Example #4
0
        public override void Run()
        {
            if (coreService.Config.HealthCheckEnabled)
            {
                bool healthCheckFailed = false;
                loggingService.Info("Health check results:");

                foreach (var healthCheck in healthCheckService.GetHealthChecks().OrderBy(c => c.Name))
                {
                    var    elapsedSinceLastUpdate = (DateTimeOffset.Now - healthCheck.LastUpdated).TotalSeconds;
                    bool   healthCheckTimeout     = coreService.Config.HealthCheckSuspendTradingTimeout > 0 && elapsedSinceLastUpdate > coreService.Config.HealthCheckSuspendTradingTimeout;
                    string indicator = (healthCheck.Failed || healthCheckTimeout) ? "[-]" : "[+]";

                    if (healthCheck.Message != null)
                    {
                        loggingService.Info($" {indicator} ({healthCheck.LastUpdated:HH:mm:ss}) {healthCheck.Name} - {healthCheck.Message}");
                    }
                    else
                    {
                        loggingService.Info($" {indicator} ({healthCheck.LastUpdated:HH:mm:ss}) {healthCheck.Name}");
                    }

                    if (healthCheck.Failed || healthCheckTimeout)
                    {
                        healthCheckFailed = true;
                    }
                }

                if (healthCheckFailed)
                {
                    healthCheckFailures++;
                }
                else
                {
                    healthCheckFailures = 0;
                }

                if (healthCheckFailed && coreService.Config.HealthCheckFailuresToRestartServices > 0 && healthCheckFailures >= coreService.Config.HealthCheckFailuresToRestartServices)
                {
                    coreService.Restart();
                }
                else
                {
                    if (healthCheckFailed && !tradingService.IsTradingSuspended)
                    {
                        loggingService.Info($"Health check failed ({healthCheckFailures})");
                        notificationService.Notify($"Health check failed ({healthCheckFailures})");
                        healthCheckService.RemoveHealthCheck(Constants.HealthChecks.TradingPairsProcessed);
                        healthCheckService.RemoveHealthCheck(Constants.HealthChecks.TradingRulesProcessed);
                        healthCheckService.RemoveHealthCheck(Constants.HealthChecks.SignalRulesProcessed);
                        tradingService.SuspendTrading();
                    }
                    else if (!healthCheckFailed && tradingService.IsTradingSuspended)
                    {
                        loggingService.Info("Health check passed");
                        notificationService.Notify("Health check passed");
                        tradingService.ResumeTrading();
                    }
                }
            }
        }