WMS_MAIN/script/qimen-watchdog.sh
liufumingren 360e47415b feat(ops): 新增 qimen 接口服务看门狗脚本(5 分钟探活 + 按 deploy 方式自愈)
生产每 5 分钟探活一次当前版本;连续 4 次探活失败则按 script/deploy-remote.cjs 完全相同的
方式恢复:释放目标端口残留进程(kill → sleep 30 → 仍占用则 kill -9 + fuser -k)→
cd <目标版本目录> && nohup bash -lc 'npm start' > /tmp/wms-<ver>-start.log 2>&1 & →
curl -fsS --connect-timeout 2 --max-time 3 健康检查 10 次 × 2s(与 deploy 的 20s 等待窗口一致)。

保活范围取 current.md 与 nginx-upstream.conf 两个来源的并集(去重):发布过程中二者会短暂
不一致,取并集可避免「nginx 实际转发的那个实例挂了却没人管」;两者不一致时打印 WARN。

两个关键设计:
- 探活失败要重试 3 次(共 6 秒)才动作:deploy 先写 current.md、再启动新实例,中间约 3 秒
  空窗,6 秒重试确认正好覆盖,避免看门狗与 deploy 同时启动同一实例;
- 启动前按目录清理孤儿 node 进程:master 被 kill -9 后 worker 会被 init 收养继续运行
  (端口已释放但进程还在),不清就再次启动会出现两份实例 → egg schedule 重复执行
  (重复推送 / 重复回信)。

其它:flock 串行化防重叠;支持 --dry-run 只检活不启动(用于验证);一次运行输出一行、
约 16 KB/天,无需轮转。

系统级定时任务放在 /etc/cron.d/qimen-watchdog 而不是 root crontab —— 宝塔面板会整体重写
root crontab,放那里会被冲掉:
  */5 * * * * root /www/wwwroot/qimen/qimen-watchdog.sh >> /www/wwwlogs/wms/qimen-watchdog.log 2>&1

服务器上的稳定副本位于 deploy 根目录 /www/wwwroot/qimen/qimen-watchdog.sh(不受版本切换与
发布清理影响,发布只清理 v1/v2 目录),本文件是它的源;更新后需重新安装到该路径。
2026-09-27 03:25:20 +08:00

230 lines
8.0 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
#
# qimen 接口服务看门狗(保活)
#
# 目的:每 5 分钟探活一次当前版本;探活失败且重试仍失败时,**按 deploy 完全相同的方式**把
# current.md 指定的版本拉起来。启动方式与 script/deploy-remote.cjs 严格对齐:
# 1) 释放目标端口残留进程(先 kill 优雅停止 → sleep 30 → 仍占用则 kill -9 + fuser -k);
# 2) cd <目标版本目录> && nohup bash -lc 'npm start' > /tmp/wms-<ver>-start.log 2>&1 &;
# 3) 健康检查 curl -fsS --connect-timeout 2 --max-time 3 http://127.0.0.1:<port>/,
# 10 次 × 2s(与 deploy 的 20s 等待窗口一致)。
#
# 保活范围(两个来源取并集,去重;正常情况下二者相同):
# - current.md → 「当前版本」
# - nginx-upstream.conf → nginx 实际转发到的版本(发布过程中可能出现短暂不一致)
# 两者不一致时打印 WARN 并同时保活,避免「nginx 指向的实例挂了却没人管」。
#
# 为什么探活要重试 3 次再动作:发布过程中 deploy 会先写 current.md、再启动新实例、最后切
# upstream。写 current.md 到新实例就绪之间有约 3 秒空窗,6 秒重试确认正好覆盖这个窗口,
# 避免看门狗与 deploy 同时启动同一实例。
#
# 为什么启动前要按目录清残留进程:若 master 被 kill -9,worker 会被 init 收养继续跑
# (端口已释放但进程还在),此时直接再启动会出现两份实例 → egg schedule 重复执行
# (重复推送/重复回信)。按 deploy 的做法先清干净再启动。
#
# 用法:
# bash qimen-watchdog.sh # 正常保活
# bash qimen-watchdog.sh --dry-run # 只探活并报告,绝不启动/杀进程(用于验证)
# QIMEN_DEPLOY_ROOT=/xxx bash ... # 覆盖部署根目录(默认 /www/wwwroot/qimen)
#
# cron(系统级,不放在 root crontab —— 宝塔面板会整体重写 root crontab,会把它冲掉):
# /etc/cron.d/qimen-watchdog
# */5 * * * * root /www/wwwroot/qimen/qimen-watchdog.sh >> /www/wwwlogs/wms/qimen-watchdog.log 2>&1
#
# 仓库源文件:script/qimen-watchdog.sh(本文件是它在服务器上的稳定副本,更新后需重新安装;
# 放 deploy 根目录而非 v1/v2 内,是为了不受版本切换与发布清理影响)。
#
set -u
DEPLOY_ROOT=${QIMEN_DEPLOY_ROOT:-/www/wwwroot/qimen}
PROBE_PATH=${QIMEN_PROBE_PATH:-/}
CURRENT_MD="$DEPLOY_ROOT/current.md"
UPSTREAM_CONF="$DEPLOY_ROOT/nginx-upstream.conf"
LOCK_FILE=${QIMEN_WATCHDOG_LOCK:-/var/lock/qimen-watchdog.lock}
TAG='[qimen-watchdog]'
DRY_RUN=0
for arg in "$@"; do
case "$arg" in
--dry-run) DRY_RUN=1 ;;
-h|--help)
sed -n '2,40p' "$0"
exit 0
;;
esac
done
now() { date '+%F %T'; }
say() { echo "$(now) $TAG $*"; }
# 版本 ↔ 端口(与 script/deploy-remote.cjs 的 getTargetPort 一致)
port_of() {
case "$1" in
v1) echo 6061 ;;
v2) echo 6062 ;;
*) echo '' ;;
esac
}
# 探活:与 deploy 的健康检查同一条命令
probe() {
curl -fsS --connect-timeout 2 --max-time 3 "http://127.0.0.1:$1$PROBE_PATH" >/dev/null 2>&1
}
# 取端口占用进程(与 deploy 的 killPortScript 同一套 ss/awk/grep 写法)
port_pid() {
ss -tlnp 2>/dev/null \
| awk -v p=":$1 " 'index($0, p) { print $0 }' \
| grep -oP 'pid=\K[0-9]+' | head -1 || true
}
# 取某版本目录下的 node 进程(与 deploy 的 stopOldScript / killNewScript 同一套写法)
dir_pids() {
ps -eo pid=,args= 2>/dev/null \
| awk -v root="$1" 'index($0, root) && $2 ~ /node$/ { print $1 }' || true
}
# ---------------------------------------------------------------------------
# 0. 串行化:上一轮没跑完就跳过,避免并发重复启动
# ---------------------------------------------------------------------------
if ! ( exec 9>"$LOCK_FILE" ) 2>/dev/null; then
say "ERROR 无法打开锁文件 $LOCK_FILE,本轮跳过"
exit 1
fi
exec 9>"$LOCK_FILE"
if ! flock -n 9; then
say "上一轮尚未结束,本轮跳过"
exit 0
fi
# ---------------------------------------------------------------------------
# 1. 确定要保活的版本集合
# ---------------------------------------------------------------------------
CURRENT_VER=$(tr -d '\r\n' < "$CURRENT_MD" 2>/dev/null || true)
UPSTREAM_PORT=$(grep -oE '127\.0\.0\.1:[0-9]+' "$UPSTREAM_CONF" 2>/dev/null | head -1 | grep -oE '[0-9]+$' || true)
case "$UPSTREAM_PORT" in
6061) UPSTREAM_VER=v1 ;;
6062) UPSTREAM_VER=v2 ;;
*) UPSTREAM_VER='' ;;
esac
TARGETS=''
for v in "$CURRENT_VER" "$UPSTREAM_VER"; do
case "$v" in
v1|v2) ;;
*) continue ;;
esac
case " $TARGETS " in
*" $v "*) ;;
*) TARGETS="$TARGETS $v" ;;
esac
done
if [ -z "$TARGETS" ]; then
say "ERROR 无法确定目标版本(current.md='$CURRENT_VER' upstream_port='$UPSTREAM_PORT'),本轮跳过"
exit 1
fi
if [ "$CURRENT_VER" != "$UPSTREAM_VER" ]; then
say "WARN current.md=$CURRENT_VER 与 upstream=$UPSTREAM_VER 不一致(可能正在发布),本轮同时保活:$TARGETS"
fi
RC=0
# ---------------------------------------------------------------------------
# 2. 逐版本探活 → 不活则按 deploy 方式启动
# ---------------------------------------------------------------------------
for ver in $TARGETS; do
port=$(port_of "$ver")
dir="$DEPLOY_ROOT/$ver"
pid=$(port_pid "$port")
if probe "$port"; then
say "OK $ver:$port 存活 (pid=${pid:-?})"
continue
fi
# 二次确认:6 秒内再探 3 次,覆盖「发布中 current.md 已写、实例还没起来」的空窗
confirmed_down=0
for _ in 1 2 3; do
sleep 2
if probe "$port"; then
confirmed_down=0
break
fi
confirmed_down=1
done
if [ "$confirmed_down" = 0 ]; then
say "OK $ver:$port 存活(首次探活失败,重试后恢复,判定为瞬时抖动)"
continue
fi
say "DOWN $ver:$port 连续 4 次探活失败(pid=${pid:-无}),开始按 deploy 相同方式恢复"
if [ "$DRY_RUN" = 1 ]; then
say "DRY-RUN 跳过启动:cd $dir && nohup bash -lc 'npm start' > /tmp/wms-$ver-start.log 2>&1 &"
RC=1
continue
fi
# 2.1 释放端口残留进程(与 deploy 的 killPortScript 一致)
if [ -n "$pid" ]; then
say "端口 $port 被进程 $pid 占用,发送优雅停止信号"
kill "$pid" 2>/dev/null || true
sleep 30
pid_check=$(port_pid "$port")
if [ -n "$pid_check" ]; then
say "端口 $port 仍有残留进程 $pid_check,强制停止"
kill -9 "$pid_check" 2>/dev/null || true
fuser -k "$port/tcp" 2>/dev/null || true
fi
else
say "端口 $port 空闲"
fi
# 2.2 端口已空闲时,清理该目录下可能被 init 收养的孤儿 node 进程
# (master 被 kill -9 后 worker 存活、端口已释放,不清会导致两份实例重复跑定时任务)
orphans=$(dir_pids "$dir")
if [ -n "$orphans" ]; then
say "清理 $ver 目录下的孤儿进程: $orphans"
# shellcheck disable=SC2086
kill $orphans 2>/dev/null || true
sleep 5
orphans_left=$(dir_pids "$dir")
if [ -n "$orphans_left" ]; then
say "孤儿进程未退出,强制停止: $orphans_left"
# shellcheck disable=SC2086
kill -9 $orphans_left 2>/dev/null || true
fi
fi
# 2.3 启动(与 deploy 完全一致:目标目录 + nohup bash -lc 'npm start' + /tmp/wms-<ver>-start.log)
if [ ! -d "$dir" ]; then
say "ERROR 目标目录不存在: $dir,无法启动"
RC=1
continue
fi
start_log="/tmp/wms-$ver-start.log"
( cd "$dir" && nohup bash -lc 'npm start' > "$start_log" 2>&1 & )
say "已在 $dir 启动 $ver,启动日志: $start_log"
# 2.4 健康检查(与 deploy 一致:10 次 × 2s)
started=0
for _ in $(seq 1 10); do
if probe "$port"; then
started=1
break
fi
sleep 2
done
if [ "$started" = 1 ]; then
say "RECOVERED $ver:$port 启动成功且探活通过 (pid=$(port_pid "$port" || true))"
else
say "ERROR $ver:$port 启动后 20s 内仍未通过探活,请检查 $start_log"
RC=1
fi
done
exit $RC