Files
CyberGril/deploy/hel913-unified/worker_ctl.sh
T

137 lines
4.6 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.
#!/usr/bin/env bash
# HEL-913 统一体验运行包 · 专属语音 worker 启停
#
# 运行解释器是 agent venv(livekit-agents 1.8.8),产品代码经 PYTHONPATH 注入。
# worker 只连接本实例 API 与本机 LiveKit。本脚本只做本实例常规启停安全:
# - 子命令只认 start|stop|status,其他一律拒绝退出 2(不会静默当 start);
# - start 前核对 pid 句柄与 worker/.env(缺文件/含未填 <...> 占位/解析失败
# 一律拒绝),重复 start 拒绝,句柄 pid 复用拒绝;
# - stop/失败收尾只杀经身份核验的进程(cmdline 含 cybergril.voice.worker
# 且 cwd 是本实例 worker 目录),绝不按进程名扫杀;
# - start 注册检查不过 → 清掉自己刚起的进程、保留日志、退出 1。
#
# 用法: [ENV=..] bash deploy/hel913-unified/worker_ctl.sh start|stop|status
# 环境变量:
# HEL913_BASE 实例根目录(含 current/ worker/ 等)
# HEL913_AGENT_VENV agent venv python(默认 /opt/voice-agent/agent/venv/bin/python)
# HEL913_WORKER_HEALTH 健康端口(默认 8082;与 worker/.env 中一致)
set -u
BASE=${HEL913_BASE:-/opt/cybergril-hel913}
VENV=${HEL913_AGENT_VENV:-/opt/voice-agent/agent/venv/bin/python}
HEALTH=${HEL913_WORKER_HEALTH:-8082}
WDIR=$BASE/worker
ENVF=$WDIR/.env
LOG=$BASE/logs/worker.log
PIDF=$BASE/logs/worker.pid
MARKER="cybergril.voice.worker"
usage() { echo "usage: [ENV=..] bash $0 start|stop|status" >&2; exit 2; }
ours() {
local p=$1 cmd cw
case "$p" in ''|*[!0-9]*) return 1 ;; esac
[ "$p" != "$$" ] || return 1
kill -0 "$p" 2>/dev/null || return 1
cmd=$(tr '\0' ' ' < "/proc/$p/cmdline" 2>/dev/null) || return 1
cw=$(readlink "/proc/$p/cwd" 2>/dev/null) || return 1
case "$cmd" in *"$MARKER"*) ;; *) return 1 ;; esac
[ "$cw" = "$WDIR" ] || return 1
return 0
}
pidfile_pid() { [ -f "$PIDF" ] && cat "$PIDF" 2>/dev/null; }
stop() {
local p
p=$(pidfile_pid)
if [ -z "$p" ]; then
echo "no pid file; nothing stopped"
return 0
fi
if kill -0 "$p" 2>/dev/null && ! ours "$p"; then
echo "REFUSED: pid $p is alive but is NOT this worker (pid reuse?) — not killing; resolve $PIDF manually" >&2
return 1
fi
if ours "$p"; then
kill "$p"
sleep 3
if ours "$p"; then kill -9 "$p"; sleep 1; fi
fi
rm -f "$PIDF"
echo "worker stopped (pid $p)"
return 0
}
start() {
local p bad
p=$(pidfile_pid)
if [ -n "$p" ] && kill -0 "$p" 2>/dev/null; then
if ours "$p"; then
echo "REFUSED: worker already running (pid $p) — stop first" >&2
else
echo "REFUSED: pid file has live pid $p that is not this worker (pid reuse?) — resolve $PIDF manually" >&2
fi
exit 1
fi
rm -f "$PIDF"
[ -d "$WDIR" ] || { echo "REFUSED: worker dir missing: $WDIR" >&2; exit 1; }
[ -f "$ENVF" ] || { echo "REFUSED: $ENVF missing (copy worker.env.template and fill it)" >&2; exit 1; }
[ -x "$VENV" ] || { echo "REFUSED: agent venv python not executable: $VENV" >&2; exit 1; }
# 占位符守门:模板 <...> 未替换一律拒绝(不让占位串进进程环境)
bad=$(grep -nE '^[A-Za-z_][A-Za-z0-9_]*=.*<' "$ENVF" || true)
if [ -n "$bad" ]; then
echo "REFUSED: $ENVF still has unfilled <...> placeholders:" >&2
echo "$bad" >&2
exit 1
fi
# 解析守门:.env 必须能作为 KEY=value 被 source(坏引号/裸空格拒绝)
if ! (set -a; . "$ENVF"; set +a) 2>/dev/null; then
echo "REFUSED: $ENVF does not parse as KEY=value lines" >&2
exit 1
fi
mkdir -p "$BASE/logs"
cd "$WDIR" || exit 1
set -a
. "$ENVF"
set +a
PYTHONPATH="$BASE/current/src" nohup "$VENV" -m cybergril.voice.worker start > "$LOG" 2>&1 &
p=$!
echo "$p" > "$PIDF"
echo "worker starting (pid $p)"
sleep 18
if grep -m1 "registered worker" "$LOG" >/dev/null; then
echo "OK: worker registered"
return 0
fi
echo "FAIL: worker did not register — full log kept at $LOG; tail:" >&2
tail -8 "$LOG" >&2
if ours "$p"; then
kill "$p"; sleep 2
ours "$p" && kill -9 "$p"
fi
rm -f "$PIDF"
echo "cleaned up failed start (log kept: $LOG)" >&2
exit 1
}
status() {
local p
p=$(pidfile_pid)
if [ -n "$p" ]; then
if ours "$p"; then echo "running pid $p (identity verified)"
elif kill -0 "$p" 2>/dev/null; then echo "pid $p alive but NOT ours (stale pid file?)"
else echo "pid $p not alive (stale pid file)"; fi
else
echo "no pid file"
fi
curl -s --max-time 3 "http://127.0.0.1:$HEALTH/" >/dev/null && echo "health ok" \
|| echo "health port not answering"
}
case "${1:-}" in
start) start ;;
stop) stop ;;
status) status ;;
*) usage ;;
esac