Wait for Flask readiness during deploy verification.

Avoid false negatives when PM2 is online but the port is not listening yet.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-08-13 20:19:47 +08:00
parent ecf35f6c09
commit fd063c56f8
+41 -3
View File
@@ -339,14 +339,15 @@ read_app_port() {
check_http() {
local url="$1"
local code
code="$(curl -sS -o /dev/null -w '%{http_code}' --connect-timeout 5 "${url}" 2>/dev/null || echo "000")"
[[ "${code}" == "200" || "${code}" == "302" || "${code}" == "301" ]]
code="$(curl -sS -o /dev/null -w '%{http_code}' --connect-timeout 5 --max-time 8 "${url}" 2>/dev/null || echo "000")"
[[ "${code}" == "200" || "${code}" == "302" || "${code}" == "301" || "${code}" == "401" || "${code}" == "403" ]]
}
verify_deployment() {
local ip="${1:-$(detect_server_ip)}"
local port
local ok=1
local attempt max_attempts=20
port="$(read_app_port)"
step "部署验收"
@@ -358,11 +359,48 @@ verify_deployment() {
pm2 list 2>/dev/null || true
fi
if check_http "http://127.0.0.1:${port}/"; then
# Flask 冷启动常需数秒;立刻 curl 易误报
echo " ... 等待页面就绪 (最多约 ${max_attempts}s,端口 ${port})"
local http_ok=0
for attempt in $(seq 1 "${max_attempts}"); do
if check_http "http://127.0.0.1:${port}/"; then
http_ok=1
break
fi
# 进程已挂掉则提前失败
if command -v pm2 >/dev/null 2>&1 && ! pm2_app_exists "${PM2_APP_NAME}"; then
echo " [✗] 等待期间 PM2 进程消失"
ok=0
break
fi
local status=""
status="$(pm2 jlist 2>/dev/null | python3 -c '
import json,sys
name="'"${PM2_APP_NAME}"'"
try:
apps=json.load(sys.stdin)
except Exception:
print(""); sys.exit(0)
for a in apps or []:
if isinstance(a,dict) and a.get("name")==name:
print((a.get("pm2_env") or {}).get("status") or "")
break
' 2>/dev/null || true)"
if [[ "${status}" == "errored" || "${status}" == "stopped" ]]; then
echo " [✗] PM2 状态为 ${status}"
ok=0
break
fi
sleep 1
done
if [[ "${http_ok}" -eq 1 ]]; then
echo " [✓] 页面可访问 (http://127.0.0.1:${port}/)"
else
echo " [✗] 页面不可访问 (http://127.0.0.1:${port}/)"
ok=0
echo " 排查: pm2 logs ${PM2_APP_NAME} --lines 50"
echo " 确认端口: grep APP_PORT ${REPO_ROOT}/.env"
fi
if [[ "${ok}" -eq 1 ]]; then