Skip to content

Commit

Permalink
detect when DB goes down after worker startup
Browse files Browse the repository at this point in the history
  • Loading branch information
Changaco committed Jan 2, 2017
1 parent 0674b4a commit 66fea08
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions liberapay/wireup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import logging
import os
import re
import signal
from subprocess import call
import traceback

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 66fea08

Please sign in to comment.