]> git.parisson.com Git - telemeta.git/commitdiff
addd wait-for-db command
authorGuillaume Pellerin <guillaume.pellerin@ircam.fr>
Sat, 2 Apr 2016 22:24:04 +0000 (00:24 +0200)
committerGuillaume Pellerin <guillaume.pellerin@ircam.fr>
Sat, 2 Apr 2016 22:24:04 +0000 (00:24 +0200)
app/deploy/app.sh
app/wait.py [deleted file]
telemeta/management/commands/wait-for-db.py [new file with mode: 0644]

index ddfba3f749bfc928b827b3dc9135b627695e8847..9593c324ddfe0d7fb8a43a402bf6fb0f01834d4c 100644 (file)
@@ -21,13 +21,11 @@ gid='www-data'
 # stating apps
 # pip install django-angular
 
-# waiting for other services
+# waiting for other network services
 sh $app/deploy/wait.sh
 
-# waiting for available database
-python $app/wait.py
-
-# django init
+# django setup
+python $manage wait-for-db
 python $manage syncdb --noinput
 python $manage migrate --noinput
 python $manage bower_install -- --allow-root
diff --git a/app/wait.py b/app/wait.py
deleted file mode 100644 (file)
index a1dc25d..0000000
+++ /dev/null
@@ -1,19 +0,0 @@
-#!/usr/bin/python
-
-import os, time
-from django.core.management import call_command
-
-up = False
-os.environ.setdefault("DJANGO_SETTINGS_MODULE", "sandbox.settings")
-
-i = 0
-while not up:
-    try:
-        call_command('syncdb', interactive=False)
-        up = True
-    except:
-        i += 1
-        print 'initialization...'
-        if i > 10:
-            raise
-        time.sleep(1)
diff --git a/telemeta/management/commands/wait-for-db.py b/telemeta/management/commands/wait-for-db.py
new file mode 100644 (file)
index 0000000..f55207c
--- /dev/null
@@ -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)