#!/usr/bin/env bash
# 检查本机 Agent /health
set -euo pipefail

HOST="${NIANNIAN_AGENT_HOST:-127.0.0.1}"
PORT="${NIANNIAN_AGENT_PORT:-18765}"
URL="http://${HOST}:${PORT}/health"
MAX_WAIT="${HEALTH_CHECK_WAIT_SEC:-30}"

deadline=$((SECONDS + MAX_WAIT))
while (( SECONDS < deadline )); do
  if curl -sf "$URL" >/dev/null 2>&1; then
    echo "OK: $URL"
    curl -sf "$URL" | head -c 400
    echo ""
    exit 0
  fi
  sleep 1
done

echo "FAIL: $URL not ready after ${MAX_WAIT}s" >&2
exit 1
