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 # Install required system dependencies
RUN apt-get update && apt-get install --no-install-recommends -y \ RUN apt-get update && apt-get install --no-install-recommends -y \
# psycopg dependencies # psycopg dependencies
libpq-dev \ libpq-dev \
wait-for-it \
# Translations dependencies # Translations dependencies
gettext \ gettext \
# cleaning up unused files # 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 # dependencies for building Python packages
build-essential \ build-essential \
# psycopg dependencies # psycopg dependencies
libpq-dev libpq-dev \
wait-for-it
# Requirements are installed here to ensure they will be cached. # Requirements are installed here to ensure they will be cached.
COPY ./requirements . COPY ./requirements .

View File

@ -16,33 +16,7 @@ if [ -z "${POSTGRES_USER}" ]; then
fi fi
export DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}" export DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}"
python << END wait-for-it "${POSTGRES_HOST}:${POSTGRES_PORT}" -t 30
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
>&2 echo 'PostgreSQL is available' >&2 echo 'PostgreSQL is available'