From 09265b608d696e8a2027d8fbf0022bada0d68211 Mon Sep 17 00:00:00 2001 From: dekun Date: Thu, 13 Aug 2026 20:21:15 +0800 Subject: [PATCH] Re-load deploy helpers after pull and wait for HTTP ready. Quick update was verifying with stale in-memory functions before Flask bound. Co-authored-by: Cursor --- deploy/lib/update.sh | 3 +++ deploy/pull_and_restart.sh | 41 +++++++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/deploy/lib/update.sh b/deploy/lib/update.sh index de36deb..03dde05 100644 --- a/deploy/lib/update.sh +++ b/deploy/lib/update.sh @@ -19,6 +19,9 @@ require_installed() { update_quick() { step "快速更新" bash "${REPO_ROOT}/deploy/pull_and_restart.sh" + # git pull 后重新加载,避免菜单进程仍用旧的 verify_deployment + # shellcheck source=common.sh + source "${LIB_DIR}/common.sh" verify_deployment "$(detect_server_ip)" || true } diff --git a/deploy/pull_and_restart.sh b/deploy/pull_and_restart.sh index e3d397e..ce0f2d4 100644 --- a/deploy/pull_and_restart.sh +++ b/deploy/pull_and_restart.sh @@ -22,7 +22,6 @@ pm2_has_app() { if ! command -v pm2 >/dev/null 2>&1; then return 1 fi - # 勿用 pm2 pid:部分版本对不存在进程仍 exit 0 if command -v python3 >/dev/null 2>&1; then pm2 jlist 2>/dev/null | python3 -c ' import json,sys @@ -38,6 +37,43 @@ sys.exit(0 if any(isinstance(a,dict) and a.get("name")==name for a in (apps or [ pm2 describe "${name}" 2>/dev/null | grep -qE 'status[[:space:]]*[|:]' } +read_port() { + local env_file="${REPO}/.env" + local port="5004" + if [[ -f "${env_file}" ]]; then + local line + line="$(grep -E '^[[:space:]]*APP_PORT=' "${env_file}" | tail -n1 || true)" + if [[ -n "${line}" ]]; then + port="${line#*=}" + port="$(echo "${port}" | tr -d '"' | tr -d "'" | xargs)" + fi + fi + echo "${port:-5004}" +} + +wait_http_ready() { + local port="$1" + local url="http://127.0.0.1:${port}/" + local i code + echo ">>> 等待 HTTP 就绪 ${url} (最多 30s)" + for i in $(seq 1 30); do + code="$(curl -sS -o /dev/null -w '%{http_code}' --connect-timeout 2 --max-time 3 "${url}" 2>/dev/null || echo "000")" + if [[ "${code}" == "200" || "${code}" == "302" || "${code}" == "301" || "${code}" == "401" || "${code}" == "403" ]]; then + echo ">>> HTTP OK (${code}) after ${i}s" + return 0 + fi + # 进程挂了就别空等 + if ! pm2_has_app "${PM2_APP_NAME}"; then + echo ">>> PM2 进程已消失" >&2 + return 1 + fi + sleep 1 + done + echo ">>> HTTP 仍未就绪 (最后 code=${code:-000})" >&2 + echo ">>> 请查看: pm2 logs ${PM2_APP_NAME} --lines 50" >&2 + return 1 +} + cd "${REPO}" echo ">>> git pull" git pull @@ -65,4 +101,7 @@ else pm2 start ecosystem.config.cjs fi +port="$(read_port)" +wait_http_ready "${port}" || true + echo "done"