From 6f3b72d6912dabe9b592cf2af3a7e2133fda12b1 Mon Sep 17 00:00:00 2001 From: jelmert Date: Tue, 27 Aug 2024 13:23:06 +0200 Subject: [PATCH] Use wait-for-it in favor of the custom python script when waiting for postgres --- .../compose/local/django/Dockerfile | 3 +- .../compose/production/django/Dockerfile | 3 +- .../compose/production/django/entrypoint | 28 +------------------ 3 files changed, 5 insertions(+), 29 deletions(-) diff --git a/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile b/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile index ddec2d4e7..c15b86113 100644 --- a/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile +++ b/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile @@ -48,7 +48,8 @@ RUN groupadd --gid 1000 dev-user \ # Install required system dependencies RUN apt-get update && apt-get install --no-install-recommends -y \ # psycopg dependencies - libpq-dev \ + libpq-dev \ + wait-for-it \ # Translations dependencies gettext \ # cleaning up unused files diff --git a/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile b/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile index 3e0859c24..70bb2f61d 100644 --- a/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile +++ b/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile @@ -37,7 +37,8 @@ RUN apt-get update && apt-get install --no-install-recommends -y \ # dependencies for building Python packages build-essential \ # psycopg dependencies - libpq-dev + libpq-dev \ + wait-for-it # Requirements are installed here to ensure they will be cached. COPY ./requirements . diff --git a/{{cookiecutter.project_slug}}/compose/production/django/entrypoint b/{{cookiecutter.project_slug}}/compose/production/django/entrypoint index dd07f2d2a..fe517f4ce 100644 --- a/{{cookiecutter.project_slug}}/compose/production/django/entrypoint +++ b/{{cookiecutter.project_slug}}/compose/production/django/entrypoint @@ -16,33 +16,7 @@ if [ -z "${POSTGRES_USER}" ]; then fi export DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}" -python << END -import sys -import time - -import psycopg - -suggest_unrecoverable_after = 30 -start = time.time() - -while True: - try: - psycopg.connect( - dbname="${POSTGRES_DB}", - user="${POSTGRES_USER}", - password="${POSTGRES_PASSWORD}", - host="${POSTGRES_HOST}", - port="${POSTGRES_PORT}", - ) - break - except psycopg.OperationalError as error: - sys.stderr.write("Waiting for PostgreSQL to become available...\n") - - if time.time() - start > suggest_unrecoverable_after: - sys.stderr.write(" This is taking longer than expected. The following exception may be indicative of an unrecoverable error: '{}'\n".format(error)) - - time.sleep(1) -END +wait-for-it "${POSTGRES_HOST}:${POSTGRES_PORT}" -t 30 >&2 echo 'PostgreSQL is available'