Commit 4d0a9ccf authored by Stefan Bienert's avatar Stefan Bienert
Browse files

Let Django wait for the database to be up and running.

parent c6ccdcfe
...@@ -37,7 +37,6 @@ db_conn = connections['default'] ...@@ -37,7 +37,6 @@ db_conn = connections['default']
try: try:
db_conn.cursor() db_conn.cursor()
except OperationalError: except OperationalError:
sys.stderr.write("Database not yet available.\n")
sys.exit(1) sys.exit(1)
except: except:
raise raise
...@@ -49,7 +48,7 @@ END ...@@ -49,7 +48,7 @@ END
# exit immediately on commands with a non-zero exit status. # exit immediately on commands with a non-zero exit status.
set -e set -e
# wait for Redis and set it up # wait for Redis
>&2 echo "Wait for Redis" >&2 echo "Wait for Redis"
until redis_ready; do until redis_ready; do
>&2 echo "Redis is not ready yet - sleep 1s" >&2 echo "Redis is not ready yet - sleep 1s"
...@@ -57,6 +56,14 @@ until redis_ready; do ...@@ -57,6 +56,14 @@ until redis_ready; do
done done
>&2 echo "Redis up and running" >&2 echo "Redis up and running"
# wait for the database
>&2 echo "Wait for database"
until postgres_ready; do
>&2 echo "Database is not ready yet - sleep 1s"
sleep 1
done
>&2 echo "Database up and running"
# Make sure the database is set up # Make sure the database is set up
>&2 echo "Assure database is set up with tables" >&2 echo "Assure database is set up with tables"
python3 manage.py makemigrations python3 manage.py makemigrations
...@@ -70,6 +77,8 @@ python3 manage.py collectstatic --noinput ...@@ -70,6 +77,8 @@ python3 manage.py collectstatic --noinput
python3 manage.py shell << END python3 manage.py shell << END
from django.contrib.auth.models import User from django.contrib.auth.models import User
try: try:
User.objects.get(username='admin')
except User.DoesNotExist:
User.objects.create_superuser('admin', 'ad@m.in', 'admin') User.objects.create_superuser('admin', 'ad@m.in', 'admin')
except Exception as dexc: except Exception as dexc:
if str(dexc) == 'UNIQUE constraint failed: auth_user.username': if str(dexc) == 'UNIQUE constraint failed: auth_user.username':
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment