Ejemplo n.º 1
0
        public async Task <IActionResult> Get()
        {
            HealthModel response;

            var item = cache.Get <HealthModel>(CACHE_KEY);

            if (item == null)
            {
                var runningTime = TimeSpan.MaxValue;
                try {
                    Process currentProcess = Process.GetCurrentProcess();
                    if (currentProcess != null)
                    {
                        runningTime = DateTime.Now.Subtract(currentProcess.StartTime);
                    }
                } catch (Exception) {
                    // ignore this, not critical
                }

                var ok = new ServiceStatus {
                    Healthy = true,
                    Status  = "OK"
                };

                var dbstatus = service.GetDatabaseStatus();
                var db       = new ServiceStatus {
                    Healthy = dbstatus,
                    Status  = dbstatus ? "OK" : "Unavailable"
                };

                response = await Task.Run(() => new HealthModel()
                {
                    Service   = "Document",
                    Database  = db,
                    Timestamp = DateTime.Now,
                    Uptime    = runningTime
                });

                var status  = db.Status;
                var healthy = db.Healthy;
                //if (healthy && (!response.Application.Healthy || !response.Document.Healthy || !response.Spectrum.Healthy)) {
                //    status = "Degraded";
                //}

                response.Status  = status;
                response.Healthy = healthy;

                // Store it in cache
                cache.Set(CACHE_KEY, response, DateTimeOffset.Now.AddSeconds(30));
            }
            else
            {
                response        = item;
                response.Cached = true;
            }

            if (response.Healthy)
            {
                return(Ok(response));
            }
            else
            {
                return(BadRequest(response));
            }
        }
        private async Task CacheHealth() {
            var stopwatch = new Stopwatch();
            stopwatch.Start();

            HealthModel response;
            var runningTime = TimeSpan.MaxValue;
            try {
                Process currentProcess = Process.GetCurrentProcess();
                if (currentProcess != null) {
                    runningTime = DateTime.UtcNow.Subtract(currentProcess.StartTime);
                }
            } catch (Exception) {
                // ignore this, not critical
            }

            var dbstatus = healthService.GetDatabaseStatus();
            var db = new ServiceStatusModel {
                Healthy = dbstatus,
                Status = dbstatus ? ServiceStatus.Ok : ServiceStatus.Failure,
                Timestamp = DateTime.UtcNow
            };

            var build = configuration.GetSection("Build").Get<BuildModel>();
            var policyServerStatus = cache.Get<ServiceStatusModel>(HealthCheckConstants.CACHE_KEY_POLICYSERVER) ?? new ServiceStatusModel();
            var idsStatus = cache.Get<ServiceStatusModel>(HealthCheckConstants.CACHE_KEY_IDS) ?? new ServiceStatusModel();

            response = await Task.Run(() => new HealthModel() {
                Build = build,
                Checks = new ChecksModel() {
                    Database = new ServiceStatusModel() {
                        Healthy = dbstatus,
                        Status = dbstatus ? ServiceStatus.Ok : ServiceStatus.Failure,
                        Timestamp = DateTime.UtcNow
                    },
                    PolicyServer = policyServerStatus,
                    IdentityServer = idsStatus
                },
                Timestamp = DateTime.UtcNow,
                Uptime = runningTime
            });

            response.Service = "WebApiStarter-api";
            var status = db.Status;
            var healthy = db.Healthy;
            if (healthy && (!response.Checks.Database.Healthy
                            || !response.Checks.PolicyServer.Healthy
                            || !response.Checks.IdentityServer.Healthy)) {
                status = ServiceStatus.Degraded;
            }

            response.Status = status;
            response.Healthy = healthy;
            response.Cached = true;

            stopwatch.Stop();
            RecordTelemetry(response.Service, stopwatch.Elapsed, healthy, JsonConvert.SerializeObject(response));

            if (status != ServiceStatus.Ok) {
                logger.LogError($"Health Check Response {JsonConvert.SerializeObject(response)}");
            }
            cache.Set(HealthCheckConstants.CACHE_KEY, response, DateTimeOffset.Now.AddSeconds(30));
        }