Skip to content

Commit

Permalink
Portable database health check (#70)
Browse files Browse the repository at this point in the history
* Portable database health check

to ensure that the Django migrations do not run until the database is
fully up.

The previous solution, to use the long form of `depends_on:` in our
compose.yaml file, isn't supported by podman-compose:
containers/podman-compose#453

* Switch to using a bash heredoc

to more readably construct the Python script to be run.
  • Loading branch information
jbradberry committed Feb 3, 2023
1 parent 3f1d3be commit c913399
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
8 changes: 2 additions & 6 deletions tools/docker-compose/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ services:
context: $PWD
dockerfile: wisdom-service.Containerfile
depends_on:
redis:
condition: service_started
db:
condition: service_healthy
- redis
- db
restart: always
volumes:
- $PWD/ansible_wisdom:/var/www/ansible_wisdom
Expand Down Expand Up @@ -43,8 +41,6 @@ services:
- POSTGRES_DB=wisdom
- POSTGRES_USER=wisdom
restart: always
healthcheck:
test: pg_isready -U wisdom
networks:
- dbnet
# Disabled because this doesn't work properly on MacOS.
Expand Down
20 changes: 20 additions & 0 deletions tools/scripts/launch-wisdom.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
#!/bin/bash
set -o errexit

database_ready() {
/var/www/venv/bin/python ansible_wisdom/manage.py shell <<EOF
import sys
from django.db import connection
try:
connection.ensure_connection()
except Exception:
sys.exit(1)
EOF
}

until database_ready
do
echo "Waiting until the database is ready..."
sleep 5
done

/var/www/venv/bin/python ansible_wisdom/manage.py migrate --noinput

cd ansible_wisdom/
Expand Down

0 comments on commit c913399

Please sign in to comment.