Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically (re)start postgres in production #462

Merged
merged 1 commit into from
Dec 23, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 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
from subprocess import check_call
import traceback

from six import text_type as str
Expand All @@ -16,6 +17,7 @@
from babel.numbers import parse_pattern
from environment import Environment, is_yesish
from mailshake import DummyMailer, SMTPMailer
import psycopg2
import raven

from liberapay import elsewhere
Expand Down Expand Up @@ -49,10 +51,25 @@ def canonical(env):
return locals()


def db(env):
def database(env, tell_sentry, retry=True):
dburl = env.database_url
maxconn = env.database_maxconn
db = DB(dburl, maxconn=maxconn)
try:
db = DB(dburl, maxconn=maxconn)
except psycopg2.OperationalError:
pg_dir = os.environ.get('OPENSHIFT_PG_DATA_DIR')
if not pg_dir:
# We're not in production, let the developer deal with it.
raise
if not retry:
# Give up
raise
try:
check_call(['pg_ctl', '-D', pg_dir, 'start', '-w', '-t', '120'])
except Exception as e:
tell_sentry(e, {})
raise
return database(env, tell_sentry, retry=False)

for model in (AccountElsewhere, Community, ExchangeRoute, Participant):
db.register_model(model)
Expand Down Expand Up @@ -408,13 +425,14 @@ def env():

minimal_algorithm = Algorithm(
env,
db,
make_sentry_teller,
database,
)

full_algorithm = Algorithm(
env,
db,
make_sentry_teller,
database,
canonical,
app_conf,
mail,
Expand Down