Provide more context when wating for PostgreSQL takes too long
@@ -16,12 +16,16 @@ if [ -z "${POSTGRES_USER}" ]; then
fi
export DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}"
-postgres_ready() {
python << END
import sys
+import time
import psycopg2
+suggest_unrecoverable_after = 30
+start = time.time()
+
+while True:
try:
psycopg2.connect(
dbname="${POSTGRES_DB}",
@@ -30,16 +34,16 @@ try:
host="${POSTGRES_HOST}",
port="${POSTGRES_PORT}",
)
-except psycopg2.OperationalError:
- sys.exit(-1)
-sys.exit(0)
+ break
+ except psycopg2.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
-}
-until postgres_ready; do
- >&2 echo 'Waiting for PostgreSQL to become available...'
- sleep 1
-done
+
>&2 echo 'PostgreSQL is available'
exec "$@"
