From 17ead2ab78093cc32f2896436ad62e727691899c Mon Sep 17 00:00:00 2001 From: Jeff Bradberry Date: Thu, 2 Feb 2023 14:45:03 -0500 Subject: [PATCH 1/2] 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: https://github.com/containers/podman-compose/pull/453 --- tools/docker-compose/compose.yaml | 8 ++------ tools/scripts/launch-wisdom.sh | 6 ++++++ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/tools/docker-compose/compose.yaml b/tools/docker-compose/compose.yaml index db6107e17..446433e44 100644 --- a/tools/docker-compose/compose.yaml +++ b/tools/docker-compose/compose.yaml @@ -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 @@ -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. diff --git a/tools/scripts/launch-wisdom.sh b/tools/scripts/launch-wisdom.sh index 34830544d..832048774 100755 --- a/tools/scripts/launch-wisdom.sh +++ b/tools/scripts/launch-wisdom.sh @@ -1,4 +1,10 @@ #!/bin/bash +until echo -e 'import sys\nfrom django.db import connection\ntry:\n connection.ensure_connection()\nexcept Exception:\n sys.exit(1)\n' | /var/www/venv/bin/python ansible_wisdom/manage.py shell +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/ From 6323267079a9a2280450af52c193a13a3a1af308 Mon Sep 17 00:00:00 2001 From: Jeff Bradberry Date: Fri, 3 Feb 2023 10:13:58 -0500 Subject: [PATCH 2/2] Switch to using a bash heredoc to more readably construct the Python script to be run. --- tools/scripts/launch-wisdom.sh | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tools/scripts/launch-wisdom.sh b/tools/scripts/launch-wisdom.sh index 832048774..7e6239929 100755 --- a/tools/scripts/launch-wisdom.sh +++ b/tools/scripts/launch-wisdom.sh @@ -1,5 +1,19 @@ #!/bin/bash -until echo -e 'import sys\nfrom django.db import connection\ntry:\n connection.ensure_connection()\nexcept Exception:\n sys.exit(1)\n' | /var/www/venv/bin/python ansible_wisdom/manage.py shell +set -o errexit + +database_ready() { +/var/www/venv/bin/python ansible_wisdom/manage.py shell <