#!/usr/bin/env bash # scripts/health-check.sh — poll /health/ready until 200 or timeout. Used as # the post-start gate by deploy.sh. Returns non-zero on any failure. set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" . "$SCRIPT_DIR/_lib.sh" usage() { cat <<'EOF' Usage: ./scripts/health-check.sh [--help] Reads from the environment: DEPLOY_HOST_PORT port the container is published on (default 4000) HEALTH_BASE_URL full URL override; defaults to http://127.0.0.1:$DEPLOY_HOST_PORT HEALTH_TIMEOUT seconds total to wait (default 60) HEALTH_INTERVAL seconds between attempts (default 2) EOF } [[ "${1:-}" == "--help" || "${1:-}" == "-h" ]] && { usage; exit 0; } require_cmd curl PORT="${DEPLOY_HOST_PORT:-4000}" BASE_URL="${HEALTH_BASE_URL:-http://127.0.0.1:$PORT}" TIMEOUT="${HEALTH_TIMEOUT:-60}" INTERVAL="${HEALTH_INTERVAL:-2}" # Liveness first (cheap; fails only if the process is wedged). log_info "Probing $BASE_URL/health/live" if ! curl --fail --silent --show-error --max-time 3 "$BASE_URL/health/live" >/dev/null; then die "/health/live did not return 200 — container is not responsive" fi log_info "Polling $BASE_URL/health/ready (timeout=${TIMEOUT}s, interval=${INTERVAL}s)" DEADLINE=$(( $(date +%s) + TIMEOUT )) ATTEMPT=0 while :; do ATTEMPT=$((ATTEMPT + 1)) if BODY="$(curl --fail --silent --show-error --max-time 3 "$BASE_URL/health/ready" 2>/dev/null)"; then log_info "/health/ready returned 200 on attempt $ATTEMPT: $BODY" exit 0 fi NOW=$(date +%s) if (( NOW >= DEADLINE )); then die "/health/ready did not return 200 within ${TIMEOUT}s (gave up after $ATTEMPT attempts)" fi sleep "$INTERVAL" done