Use wait-for-it in favor of the custom python script when waiting for postgres

This commit is contained in:
jelmert 2024-08-27 13:23:06 +02:00
parent 2ff8a5f0d2
commit 6f3b72d691
3 changed files with 5 additions and 29 deletions

View File

@ -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

View File

@ -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 .

View File

@ -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'