跨 Job 累计技术失败次数,耗尽后将 Attempt/Gate 置为 failed_terminal; 收缩旧 action_buttons;healthcheck 增加 Worker 领取探针;恢复卡展示责任方与审计,Admin 诊断改主从布局。 Co-authored-by: Cursor <cursoragent@cursor.com>
139 lines
4.8 KiB
Bash
Executable File
139 lines
4.8 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
#
|
|
# scripts/healthcheck.sh
|
|
# 用途:部署后健康检查——核心服务运行中,并校验 shipping-worker 可感知 Process Gate 配置。
|
|
# 依赖: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
|
|
|
|
# [STEP] Process Gate:确认 shipping-worker 注入了租约/心跳环境变量(领取 Job 的前提配置)。
|
|
gate_env=$("${DC_CMD[@]}" "${compose_args[@]}" exec -T hxops-shipping-worker \
|
|
sh -c 'printf "%s" "${SHIPPING_PROCESS_GATE_LEASE_SECONDS:-}"' 2>/dev/null || true)
|
|
if [[ -z "${gate_env}" ]]; then
|
|
echo "Healthcheck failed: hxops-shipping-worker missing SHIPPING_PROCESS_GATE_LEASE_SECONDS"
|
|
exit 1
|
|
fi
|
|
echo "Process Gate worker env OK (LEASE_SECONDS=${gate_env})."
|
|
|
|
# [STEP] 迁移门禁:确认门禁表已存在(Alembic upgrade 后应有 shipping_process_executions)。
|
|
table_check=$("${DC_CMD[@]}" "${compose_args[@]}" exec -T hxops-backend \
|
|
python - <<'PY' 2>/dev/null || true
|
|
from app import create_app, db
|
|
from sqlalchemy import inspect
|
|
app = create_app()
|
|
with app.app_context():
|
|
names = set(inspect(db.engine).get_table_names())
|
|
need = {
|
|
'shipping_process_executions',
|
|
'shipping_process_gates',
|
|
'shipping_process_gate_attempts',
|
|
}
|
|
missing = sorted(need - names)
|
|
print('ok' if not missing else 'missing:' + ','.join(missing))
|
|
PY
|
|
)
|
|
if [[ "${table_check}" != "ok" ]]; then
|
|
echo "Healthcheck failed: Process Gate tables not ready (${table_check:-empty})"
|
|
exit 1
|
|
fi
|
|
echo "Process Gate migration tables OK."
|
|
|
|
# [STEP] 领取能力:确认 JobType 已注册且 shipping_process_gate handler 可导入。
|
|
handler_check=$("${DC_CMD[@]}" "${compose_args[@]}" exec -T hxops-shipping-worker \
|
|
python - <<'PY' 2>/dev/null || true
|
|
from app.jobs.types import JobType
|
|
from app.jobs.registry import get_handler
|
|
import app.jobs.handlers # noqa: F401 # 注册全部 handler
|
|
handler = get_handler(JobType.SHIPPING_PROCESS_GATE)
|
|
print('ok' if callable(handler) else 'missing_handler')
|
|
PY
|
|
)
|
|
if [[ "${handler_check}" != "ok" ]]; then
|
|
echo "Healthcheck failed: shipping_process_gate handler not claimable (${handler_check:-empty})"
|
|
exit 1
|
|
fi
|
|
echo "Process Gate job handler OK."
|
|
|
|
# [STEP] 真实领取:入队探针 Job,等待 shipping-worker 将其推进到终态。
|
|
probe_check=$("${DC_CMD[@]}" "${compose_args[@]}" exec -T hxops-backend \
|
|
python - <<'PY' 2>/dev/null || true
|
|
import time
|
|
from app import create_app, db
|
|
from app.models import Job
|
|
from app.services.shipping_notice.process_gate_job_service import enqueue_process_gate_healthcheck_probe
|
|
|
|
app = create_app()
|
|
with app.app_context():
|
|
job_id = enqueue_process_gate_healthcheck_probe()
|
|
deadline = time.time() + 45
|
|
while time.time() < deadline:
|
|
db.session.expire_all()
|
|
job = db.session.get(Job, int(job_id))
|
|
if job is None:
|
|
print('missing_job')
|
|
raise SystemExit(0)
|
|
status = str(job.status or '')
|
|
if status in {'succeeded', 'failed', 'cancelled'}:
|
|
print('ok:' + status)
|
|
raise SystemExit(0)
|
|
if status == 'running':
|
|
# 已被 Worker 领取即视为通过(终态可能稍晚)。
|
|
print('ok:running')
|
|
raise SystemExit(0)
|
|
time.sleep(1.5)
|
|
print('timeout:queued')
|
|
PY
|
|
)
|
|
if [[ "${probe_check}" != ok:* ]]; then
|
|
echo "Healthcheck failed: Process Gate worker did not claim probe job (${probe_check:-empty})"
|
|
exit 1
|
|
fi
|
|
echo "Process Gate worker claim probe OK (${probe_check})."
|
|
|
|
echo "Healthcheck passed for ${ENV_NAME} environment."
|
|
exit 0
|