55 lines
1.6 KiB
Bash
Executable File
55 lines
1.6 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
#
|
|
# scripts/healthcheck.sh
|
|
# 用途:部署后基础健康检查——确认本次 Compose 服务已启动。
|
|
# 依赖:docker compose、docker-compose.yml、可选 .env 文件。
|
|
# 逻辑:由 deploy_compose.sh 在 compose up 后调用;模块专项能力由应用运行时按需校验。
|
|
#
|
|
set -euo pipefail
|
|
|
|
ENV_NAME="${1:-default}"
|
|
COMPOSE_FILE="${2:?Compose file path required}"
|
|
ENV_FILE="${3:-}"
|
|
|
|
if docker compose version >/dev/null 2>&1; then
|
|
DC_CMD=(docker compose)
|
|
elif command -v docker-compose >/dev/null 2>&1; then
|
|
DC_CMD=(docker-compose)
|
|
else
|
|
echo "Missing docker compose command. Install docker compose plugin or docker-compose."
|
|
exit 1
|
|
fi
|
|
|
|
declare -a compose_args=(-f "${COMPOSE_FILE}")
|
|
if [[ -n "${ENV_FILE}" && -f "${ENV_FILE}" ]]; then
|
|
compose_args+=(--env-file "${ENV_FILE}")
|
|
fi
|
|
|
|
max_attempts=20
|
|
sleep_seconds=3
|
|
required_services=(hxops-backend hxops-shipping-worker)
|
|
|
|
for ((i=1; i<=max_attempts; i++)); do
|
|
running=$("${DC_CMD[@]}" "${compose_args[@]}" ps --status running --services || true)
|
|
missing=()
|
|
for svc in "${required_services[@]}"; do
|
|
if ! grep -qx "${svc}" <<<"${running}"; then
|
|
missing+=("${svc}")
|
|
fi
|
|
done
|
|
if [[ ${#missing[@]} -eq 0 ]]; then
|
|
echo "Core services running for ${ENV_NAME}."
|
|
break
|
|
fi
|
|
if [[ "${i}" -eq "${max_attempts}" ]]; then
|
|
echo "Healthcheck failed for ${ENV_NAME}: missing ${missing[*]}"
|
|
"${DC_CMD[@]}" "${compose_args[@]}" ps
|
|
exit 1
|
|
fi
|
|
echo "Healthcheck waiting (${i}/${max_attempts}): missing ${missing[*]}..."
|
|
sleep "${sleep_seconds}"
|
|
done
|
|
|
|
echo "Healthcheck passed for ${ENV_NAME} environment."
|
|
exit 0
|