From d5f874c6a8c6a8e57eb4f7d95c3dcc98d47cfb7d Mon Sep 17 00:00:00 2001 From: Lukas Holecek Date: Oct 05 2018 10:15:25 +0000 Subject: Check for locked DB in healthcheck If database is locked for too long the healthcheck request should timeout on client side (e.g. `livenessProbe` in OpenShift). Signed-off-by: Lukas Holecek --- diff --git a/waiverdb/app.py b/waiverdb/app.py index c708fac..f1546e2 100644 --- a/waiverdb/app.py +++ b/waiverdb/app.py @@ -10,6 +10,7 @@ except ImportError: from flask import Flask, current_app from flask_migrate import Migrate from sqlalchemy import event +from sqlalchemy.exc import ProgrammingError import requests from waiverdb.events import publish_new_waiver @@ -118,9 +119,12 @@ def healthcheck(): Returns a 200 response if the application is alive and able to serve requests. """ - result = db.session.execute('SELECT 1').scalar() - if result != 1: + try: + db.session.execute("SELECT 1 FROM waiver LIMIT 0").fetchall() + except ProgrammingError: + current_app.logger.exception('Healthcheck failed on DB query.') raise RuntimeError('Unable to communicate with database.') + return ('Health check OK', 200, [('Content-Type', 'text/plain')])