fiddy/scripts/wait-for-health.sh

32 lines
937 B
Bash

#!/usr/bin/env bash
set -euo pipefail
HEALTH_URL="${1:-${HEALTH_URL:-}}"
MAX_ATTEMPTS="${MAX_ATTEMPTS:-30}"
SLEEP_SECONDS="${SLEEP_SECONDS:-10}"
if [[ -z "$HEALTH_URL" ]]; then
echo "Usage: HEALTH_URL=https://your-domain/api/health/ready scripts/wait-for-health.sh"
echo " or: scripts/wait-for-health.sh https://your-domain/api/health/ready"
exit 1
fi
tmp_body="$(mktemp)"
trap 'rm -f "$tmp_body"' EXIT
for attempt in $(seq 1 "$MAX_ATTEMPTS"); do
status="$(curl -sS -o "$tmp_body" -w "%{http_code}" "$HEALTH_URL" || true)"
if [[ "$status" == "200" ]] && grep -q '"request_id"' "$tmp_body"; then
echo "Health check passed on attempt $attempt/$MAX_ATTEMPTS"
exit 0
fi
echo "Attempt $attempt/$MAX_ATTEMPTS failed (status=$status), retrying in ${SLEEP_SECONDS}s..."
sleep "$SLEEP_SECONDS"
done
echo "Health check failed after $MAX_ATTEMPTS attempts."
echo "Last response body:"
cat "$tmp_body" || true
exit 1