From 66fea0851636112cfbc9a4a6fe9ed94cc8cbb7ba Mon Sep 17 00:00:00 2001 From: Changaco Date: Mon, 2 Jan 2017 13:04:05 +0100 Subject: [PATCH] detect when DB goes down after worker startup --- liberapay/wireup.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/liberapay/wireup.py b/liberapay/wireup.py index 0361f04bd7..71db831dc0 100644 --- a/liberapay/wireup.py +++ b/liberapay/wireup.py @@ -5,6 +5,7 @@ import logging import os import re +import signal from subprocess import call import traceback @@ -233,6 +234,19 @@ def tell_sentry(exception, state, allow_reraise=True): # Don't flood Sentry when DB is down return + if isinstance(exception, psycopg2.Error): + from liberapay.website import website + if getattr(website, 'db', None): + try: + website.db.one('SELECT 1 AS x') + except psycopg2.Error: + # If it can't answer this simple query, it's down. + website.db = NoDB() + # Show the proper 503 error page + state['exception'] = NeedDatabase() + # Tell gunicorn to gracefully restart this worker + os.kill(os.getpid(), signal.SIGTERM) + if not sentry: if env.sentry_reraise and allow_reraise: raise