Provide more context when wating for PostgreSQL takes too long (#3782)

This commit is contained in:
jugglinmike 2022-07-26 18:17:45 -04:00 committed by GitHub
parent 119a282dad
commit 903fe45eca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -16,13 +16,17 @@ 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}"
postgres_ready() {
python << END python << END
import sys import sys
import time
import psycopg2 import psycopg2
try: suggest_unrecoverable_after = 30
start = time.time()
while True:
try:
psycopg2.connect( psycopg2.connect(
dbname="${POSTGRES_DB}", dbname="${POSTGRES_DB}",
user="${POSTGRES_USER}", user="${POSTGRES_USER}",
@ -30,16 +34,16 @@ try:
host="${POSTGRES_HOST}", host="${POSTGRES_HOST}",
port="${POSTGRES_PORT}", port="${POSTGRES_PORT}",
) )
except psycopg2.OperationalError: break
sys.exit(-1) except psycopg2.OperationalError as error:
sys.exit(0) 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 END
}
until postgres_ready; do
>&2 echo 'Waiting for PostgreSQL to become available...'
sleep 1
done
>&2 echo 'PostgreSQL is available' >&2 echo 'PostgreSQL is available'
exec "$@" exec "$@"