From: Guillaume Pellerin Date: Tue, 19 Apr 2016 10:32:43 +0000 (+0200) Subject: add command waiting for db X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=764ca3361c0b1240d81402a64332a618ddab9d17;p=mezzo.git add command waiting for db --- diff --git a/app/festival/management/commands/wait-for-db-connection.py b/app/festival/management/commands/wait-for-db-connection.py deleted file mode 100644 index 71f3b033..00000000 --- a/app/festival/management/commands/wait-for-db-connection.py +++ /dev/null @@ -1,16 +0,0 @@ -from django.conf import settings -from django.core.management.base import BaseCommand, CommandError -from django.db import connection - - -class Command(BaseCommand): - help = "Wait for database connection" - - def handle(self, *args, **options): - up = False - while not up: - try: - cursor = connection.cursor() - up = True - except: - time.sleep(1) diff --git a/app/festival/management/commands/wait-for-db.py b/app/festival/management/commands/wait-for-db.py new file mode 100644 index 00000000..f55207c9 --- /dev/null +++ b/app/festival/management/commands/wait-for-db.py @@ -0,0 +1,30 @@ +import os, time + +from optparse import make_option +from django.conf import settings +from django.core.management.base import BaseCommand, CommandError +from django.db import connections + + +class Command(BaseCommand): + help = "wait for default DB connection" + + db_name = 'default' + N = 10 + + def handle(self, *args, **options): + i = 0 + connected = False + db_conn = connections[self.db_name] + while not connected: + try: + c = db_conn.cursor() + connected = True + except: + print('error connecting to DB...') + if i > self.N: + print('...exiting') + raise + print('...retrying') + i += 1 + time.sleep(1) diff --git a/app/scripts/app.sh b/app/scripts/app.sh index 962cf9a7..5d6c984c 100644 --- a/app/scripts/app.sh +++ b/app/scripts/app.sh @@ -26,12 +26,8 @@ chown -R $uid:$gid $media # waiting for other services sh $app/scripts/wait.sh -# waiting for available database -# python $app/wait.py -# python $manage wait-for-db-connection - -# django init -# python $manage syncdb --noinput +# django setup +python $manage wait-for-db python $manage migrate --noinput python $manage collectstatic --noinput python $manage create-admin-user