From f543bb2646808f419b4df3d7216b759393b04985 Mon Sep 17 00:00:00 2001 From: "Peter A. Jonsson" Date: Wed, 7 Aug 2024 00:05:10 +0200 Subject: [PATCH] compose: add health-check to postgres Add a health check to compose and use that in the CI instead of the wait-for-db-script. --- .github/workflows/complementary-config-test.yaml | 3 +-- .github/workflows/pyspy-profiling.yaml | 7 +------ .github/workflows/test-prod.yaml | 7 +------ .github/workflows/test.yml | 2 +- check-code-all.sh | 2 -- docker-compose.cleandb.yaml | 9 +++++++++ docker-compose.db.yaml | 9 +++++++++ docker-compose.yaml | 3 +-- docker/ows/wait-for-db | 11 ----------- 9 files changed, 23 insertions(+), 30 deletions(-) delete mode 100755 docker/ows/wait-for-db diff --git a/.github/workflows/complementary-config-test.yaml b/.github/workflows/complementary-config-test.yaml index 5e13b55c..a75cc474 100644 --- a/.github/workflows/complementary-config-test.yaml +++ b/.github/workflows/complementary-config-test.yaml @@ -60,8 +60,7 @@ jobs: export LOCAL_GID=$(id -g $USER) cd ./datacube-ows export $(grep -v '^#' ./complementary_config_test/.env_complementary_config_dea_dev | xargs) - docker compose -f docker-compose.yaml -f docker-compose.cleandb.yaml up -d - docker compose -f docker-compose.yaml -f docker-compose.cleandb.yaml exec -T ows_18 /bin/sh -c "cd /code; sh ./docker/ows/wait-for-db" + docker compose -f docker-compose.yaml -f docker-compose.cleandb.yaml up -d --wait docker compose -f docker-compose.yaml -f docker-compose.cleandb.yaml exec -T ows_18 /bin/sh -c "datacube system init; datacube system check" docker compose -f docker-compose.yaml -f docker-compose.cleandb.yaml exec -T ows_18 /bin/sh -c "curl https://raw.githubusercontent.com/GeoscienceAustralia/dea-config/master/dev/services/wms/inventory.json -o /tmp/inventory.json" docker compose -f docker-compose.yaml -f docker-compose.db.yaml exec -T ows_18 /bin/sh -c "cd /code; ./compare-cfg.sh" diff --git a/.github/workflows/pyspy-profiling.yaml b/.github/workflows/pyspy-profiling.yaml index 92ffdd04..5c9b8b2a 100644 --- a/.github/workflows/pyspy-profiling.yaml +++ b/.github/workflows/pyspy-profiling.yaml @@ -42,12 +42,7 @@ jobs: export LOCAL_UID=$(id -u $USER) export LOCAL_GID=$(id -g $USER) export $(grep -v '^#' .env_simple | xargs) - docker compose -f docker-compose.yaml -f docker-compose.db.yaml -f docker-compose.pyspy.yaml up -d - - - name: Sleep for 10 seconds (stage 1 - wait for services to be ready) - uses: whatnick/wait-action@master - with: - time: '10s' + docker compose -f docker-compose.yaml -f docker-compose.db.yaml -f docker-compose.pyspy.yaml up -d --wait - name: set output container pid (stage 1 - get ows container pid) id: set-output-container-id diff --git a/.github/workflows/test-prod.yaml b/.github/workflows/test-prod.yaml index 7af740cc..bae2251f 100644 --- a/.github/workflows/test-prod.yaml +++ b/.github/workflows/test-prod.yaml @@ -57,12 +57,7 @@ jobs: export LOCAL_UID=$(id -u $USER) export LOCAL_GID=$(id -g $USER) export $(grep -v '^#' .env_simple | xargs) - docker compose -f docker-compose.yaml -f docker-compose.db.yaml -f docker-compose.prod.yaml up -d - - - name: Sleep for 10 seconds - uses: whatnick/wait-action@master - with: - time: '10s' + docker compose -f docker-compose.yaml -f docker-compose.db.yaml -f docker-compose.prod.yaml up -d --wait # Run some tests on the images # These tests require a working database diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c156fd45..d2a46e28 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -62,7 +62,7 @@ jobs: export LOCAL_UID=$(id -u $USER) export LOCAL_GID=$(id -g $USER) export $(grep -v '^#' .env_simple | xargs) - docker compose -f docker-compose.yaml -f docker-compose.db.yaml up -d + docker compose -f docker-compose.yaml -f docker-compose.db.yaml up -d --wait docker compose -f docker-compose.yaml -f docker-compose.db.yaml exec -T ows_18 /bin/sh -c "cd /code;./check-code-all.sh" docker compose -f docker-compose.yaml -f docker-compose.db.yaml down diff --git a/check-code-all.sh b/check-code-all.sh index 8ac70aa0..fba63de0 100755 --- a/check-code-all.sh +++ b/check-code-all.sh @@ -2,8 +2,6 @@ # Convenience script for running Travis-like checks. set -ex -# ensure db is ready -sh ./docker/ows/wait-for-db # Run tests, taking coverage. # Users can specify extra folders as arguments. diff --git a/docker-compose.cleandb.yaml b/docker-compose.cleandb.yaml index 5a771f79..b9c7fb5e 100644 --- a/docker-compose.cleandb.yaml +++ b/docker-compose.cleandb.yaml @@ -2,6 +2,7 @@ services: postgres: # clean postgis db image: kartoza/postgis:13-3.1 + hostname: postgres environment: - POSTGRES_DB=${DB_DATABASE} - POSTGRES_PASSWORD=${DB_PASSWORD} @@ -9,9 +10,17 @@ services: ports: - "${DB_PORT}:5432" restart: always + healthcheck: + test: ["CMD", "pg_isready", "-h", "postgres", "-q", "-d", "$$POSTGRES_DB", "-U", "$$POSTGRES_USER"] + timeout: 45s + interval: 10s + retries: 10 # Overwrite ows so it can talk to docker db ows_18: ports: - 8000:8000 environment: DB_PORT: 5432 + depends_on: + postgres: + condition: service_health diff --git a/docker-compose.db.yaml b/docker-compose.db.yaml index e1c6d758..00d1c0fc 100644 --- a/docker-compose.db.yaml +++ b/docker-compose.db.yaml @@ -2,6 +2,7 @@ services: postgres: # db with some data from s2_l2a pre-indexed build: docker/database/ + hostname: postgres environment: - POSTGRES_DB=${DB_DATABASE} - POSTGRES_PASSWORD=${DB_PASSWORD} @@ -9,9 +10,17 @@ services: ports: - "${DB_PORT}:5432" restart: always + healthcheck: + test: ["CMD", "pg_isready", "-h", "postgres", "-q", "-d", "$$POSTGRES_DB", "-U", "$$POSTGRES_USER"] + timeout: 45s + interval: 10s + retries: 10 # Overwrite ows so it can talk to docker db ows_18: ports: - 8000:8000 environment: DB_PORT: 5432 + depends_on: + postgres: + condition: service_healthy diff --git a/docker-compose.yaml b/docker-compose.yaml index ba687afb..dec1fbc7 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -38,8 +38,7 @@ services: SENTRY_DSN: "${SENTRY_DSN}" volumes: - ${OWS_CFG_DIR}:${OWS_CFG_MOUNT_DIR} - - ./docker/ows/wait-for-db:/usr/local/bin/wait-for-db - ./:/code/ - ./artifacts:/mnt/artifacts restart: always - command: ["wait-for-db", "flask", "run", "--host=0.0.0.0", "--port=8000"] + command: ["flask", "run", "--host=0.0.0.0", "--port=8000"] diff --git a/docker/ows/wait-for-db b/docker/ows/wait-for-db deleted file mode 100755 index 732e4970..00000000 --- a/docker/ows/wait-for-db +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/sh - -RETRIES=10 - -# Wait until Database is ready -until pg_isready --dbname=$DB_DATABASE --host=$DB_HOSTNAME --port=$DB_PORT --username=$DB_USERNAME || [ $RETRIES -eq 0 ]; do - echo "Waiting for $DB_HOSTNAME server, $((RETRIES-=1)) remaining attempts..." - sleep 2 -done - -exec "$@"