Skip to content

Commit c913399

Browse files
authored
Portable database health check (#70)
* 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.
1 parent 3f1d3be commit c913399

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

tools/docker-compose/compose.yaml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ services:
66
context: $PWD
77
dockerfile: wisdom-service.Containerfile
88
depends_on:
9-
redis:
10-
condition: service_started
11-
db:
12-
condition: service_healthy
9+
- redis
10+
- db
1311
restart: always
1412
volumes:
1513
- $PWD/ansible_wisdom:/var/www/ansible_wisdom
@@ -43,8 +41,6 @@ services:
4341
- POSTGRES_DB=wisdom
4442
- POSTGRES_USER=wisdom
4543
restart: always
46-
healthcheck:
47-
test: pg_isready -U wisdom
4844
networks:
4945
- dbnet
5046
# Disabled because this doesn't work properly on MacOS.

tools/scripts/launch-wisdom.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
11
#!/bin/bash
2+
set -o errexit
3+
4+
database_ready() {
5+
/var/www/venv/bin/python ansible_wisdom/manage.py shell <<EOF
6+
import sys
7+
from django.db import connection
8+
9+
try:
10+
connection.ensure_connection()
11+
except Exception:
12+
sys.exit(1)
13+
EOF
14+
}
15+
16+
until database_ready
17+
do
18+
echo "Waiting until the database is ready..."
19+
sleep 5
20+
done
21+
222
/var/www/venv/bin/python ansible_wisdom/manage.py migrate --noinput
323

424
cd ansible_wisdom/

0 commit comments

Comments
 (0)