#!/usr/bin/env bash
set -e

max_attempts=30
sleep_seconds=5
attempts=0
until pg_isready -h "$PGHOST" -p "$PGPORT"; do
    attempts=$((attempts+1))
    if [[ "$attempts" -ge $max_attempts ]]; then
        log-entrypoint 'Database is not reachable after 30 attempts.'
        exit 1
    fi
    log-entrypoint "Waiting for database... ($attempts)"
    sleep $sleep_seconds
done
