From 8b5c781f8412bec5798eb502aa7d133c18092958 Mon Sep 17 00:00:00 2001 From: Jake Moshenko Date: Tue, 13 May 2014 18:53:42 -0400 Subject: [PATCH] Change the health check to ping the db and the redis build logs. --- data/buildlogs.py | 7 +++++++ data/model/legacy.py | 8 ++++++++ endpoints/web.py | 15 ++++++++++++--- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/data/buildlogs.py b/data/buildlogs.py index 033a6a043..c6acfd41b 100644 --- a/data/buildlogs.py +++ b/data/buildlogs.py @@ -73,6 +73,13 @@ class RedisBuildLogs(object): return json.loads(fetched) if fetched else None + def check_health(self): + try: + return self._redis.ping() == True + except redis.ConnectionError: + return False + + class BuildLogs(object): def __init__(self, app=None): self.app = app diff --git a/data/model/legacy.py b/data/model/legacy.py index 1c207cb79..bff044d5b 100644 --- a/data/model/legacy.py +++ b/data/model/legacy.py @@ -1647,3 +1647,11 @@ def delete_user(user): user.delete_instance(recursive=True, delete_nullable=True) # TODO: also delete any repository data associated + +def check_health(): + # We will connect to the db, check that it contains some log entry kinds + try: + found_count = LogEntryKind.select().count() + return found_count > 0 + except: + return False diff --git a/endpoints/web.py b/endpoints/web.py index 560fac724..8bef62397 100644 --- a/endpoints/web.py +++ b/endpoints/web.py @@ -2,13 +2,13 @@ import logging import os from flask import (abort, redirect, request, url_for, make_response, Response, - Blueprint, send_from_directory) + Blueprint, send_from_directory, jsonify) from flask.ext.login import current_user from urlparse import urlparse from data import model from data.model.oauth import DatabaseAuthorizationProvider -from app import app, billing as stripe +from app import app, billing as stripe, build_logs from auth.auth import require_session_login from auth.permissions import AdministerOrganizationPermission from util.invoice import renderInvoiceToPdf @@ -139,7 +139,16 @@ def v1(): @web.route('/status', methods=['GET']) @no_cache def status(): - return make_response('Healthy') + db_healthy = model.check_health() + buildlogs_healthy = build_logs.check_health() + + response = jsonify({ + 'db_healthy': db_healthy, + 'buildlogs_healthy': buildlogs_healthy, + }) + response.status_code = 200 if db_healthy and buildlogs_healthy else 503 + + return response @web.route('/tos', methods=['GET'])