Files
CyberGril/deploy/hel913-unified/force_end.template.sh
T

71 lines
3.1 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 运行包 · 窗口到期强制收尾模板(脱敏待批件;默认不布防)
#
# 用途:获批体验/语音窗口到截止时点的**可执行**停服与封窗:先经统一实例
# API 逐个 DELETE 本实例房间(欠清理的房间会留在登记账重试),再按专属
# pid 句柄停 worker → api → wss 桥 → 共享 TTS(都不是我们的就如实报告,
# 绝不按进程名/端口扫杀,绝不触碰 8300/8310/8320/8350、7880/7881、10095、
# 8081 等共享服务)。改编自 HEL-910 已实测 force_end.sh/watchdog.sh。
#
# 用法:
# bash force_end.template.sh now # 立即收尾(幂等,可重复)
# bash force_end.template.sh watch <epoch秒> # 到点自动收尾(受监督后台任务;
# # 更可靠的等价物是获批后装
# # systemd timer 调本脚本 now)
# 环境变量: HEL913_BASE / HEL913_HTTP(8360)
set -u
set -o pipefail
BASE=${HEL913_BASE:-/opt/cybergril-hel913}
HTTP=${HEL913_HTTP:-8360}
PKG=$BASE/current/deploy/hel913-unified
LOG=$BASE/logs/force_end.log
usage() { echo "usage: [ENV=..] bash $0 now|watch <deadline_epoch_seconds>" >&2; exit 2; }
force_end() {
mkdir -p "$BASE/logs"
ts() { date -u +%FT%TZ; }
{
echo "[$(ts)] force_end begin"
# 1) 结清本实例房间(DELETE 走统一实例所有者路径;失败房间留账重试)
for sid in $(python3 - "$BASE/data" <<'EOF' 2>/dev/null
import json, sys, pathlib
p = pathlib.Path(sys.argv[1]) / "companion-voice-sessions.json"
try:
d = json.loads(p.read_text())
except Exception:
raise SystemExit
# VoiceSessionService._persist_state stores sessions as a LIST, not a dict.
# Debts already follow bounded SDK cleanup; do not reset their retry budgets here.
for entry in d.get("sessions", []):
sid = entry.get("session_id", "")
if sid and all(c.isalnum() or c in "-_" for c in sid):
print(sid)
EOF
); do
code=$(curl -s -o /dev/null -w '%{http_code}' --max-time 8 -X DELETE "http://127.0.0.1:$HTTP/api/voice/session/$sid" || echo err)
echo "[$(ts)] DELETE session $sid -> $code"
done
# 2) 按专属句柄逐个停(每步幂等;不是我们的如实报告)
bash "$PKG/worker_ctl.sh" stop 2>&1 | sed 's/^/ worker: /'
bash "$PKG/api_ctl.sh" stop 2>&1 | sed 's/^/ api: /'
bash "$PKG/bridge_ctl.template.sh" stop 2>&1 | sed 's/^/ bridge: /'
bash "$PKG/tts_ctl.template.sh" stop 2>&1 | sed 's/^/ tts: /'
echo "[$(ts)] force_end done(封账归档另由 seal_budget_hel879.py 按账执行,封账不解除)"
} >> "$LOG" 2>&1
}
case "${1:-}" in
now) force_end ;;
watch)
DL=${2:?deadline epoch seconds required}
case "$DL" in *[!0-9]*|'') usage ;; esac
mkdir -p "$BASE/logs"
echo "[$(date -u +%FT%TZ)] watch start, deadline $(date -u -d "@$DL" +%FT%TZ) (pid $$)" >> "$LOG"
while [ "$(date +%s)" -lt "$DL" ]; do sleep 30; done
echo "[$(date -u +%FT%TZ)] DEADLINE REACHED — force end" >> "$LOG"
force_end
;;
*) usage ;;
esac