douyinWEB/scripts/_selftest_graceful_end_neko7.py
2026-09-13 22:10:49 -05:00

127 lines
4.4 KiB
Python

# -*- coding: utf-8 -*-
"""Full graceful-end selftest on dylive neko-7 (avoid pkill matching this script)."""
from __future__ import annotations
import sys
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(
"107.173.30.245",
22,
"root",
"Xiao4669805",
timeout=45,
allow_agent=False,
look_for_keys=False,
banner_timeout=120,
)
# Upload runner script to avoid pkill -f matching inline heredoc with rtmps URL
runner = r'''#!/bin/bash
set -euo pipefail
NAME=douyinweb-neko-7
NEKO_ID=7
CHANNEL_PK=82102486-f5d7-434c-b508-4c6a10c5f325
echo '=== 1 BEFORE scrape ==='
docker exec -i "$NAME" python3 - < /opt/douyinWEB/backend/neko/cdp_studio_scrape.py | tee /tmp/before.json | python3 -c 'import sys,json;d=json.load(sys.stdin);print({k:d.get(k) for k in ("ok","live_status","on_air","health","url")})'
echo '=== 2 stop ffmpeg by PID ==='
python3 - <<'PY'
import os, signal, subprocess
out=subprocess.check_output(["ps","-eo","pid,args"], text=True, errors="replace")
killed=0
for line in out.splitlines():
if "ffmpeg" in line and "youtube" in line.lower() and "python" not in line:
pid=int(line.split(None,1)[0])
print("kill", pid, line[:120])
try:
os.kill(pid, signal.SIGTERM)
killed+=1
except ProcessLookupError:
pass
print("killed", killed)
PY
sleep 2
pgrep -af '[f]fmpeg' | head -3 || echo 'ffmpeg_gone'
echo '=== 3 wait encoder lost ==='
sleep 8
docker exec -i "$NAME" python3 - < /opt/douyinWEB/backend/neko/cdp_studio_scrape.py | tee /tmp/mid.json | python3 -c 'import sys,json;d=json.load(sys.stdin);print({k:d.get(k) for k in ("ok","live_status","on_air","health")})'
echo '=== 4 end_live_session ==='
cd /opt/douyinWEB/backend
./.venv/bin/python - <<PY
import json
from app import youtube_neko
res = youtube_neko.end_live_session(
$NEKO_ID,
channel_pk="$CHANNEL_PK",
wait_encoder_lost_sec=6.0,
settle_sec=4.0,
)
open("/tmp/end_live.json","w",encoding="utf-8").write(json.dumps(res,ensure_ascii=False,indent=2))
print(json.dumps(res, ensure_ascii=False, indent=2)[:5000])
PY
echo '=== 5 AFTER scrape ==='
sleep 2
docker exec -i "$NAME" python3 - < /opt/douyinWEB/backend/neko/cdp_studio_scrape.py | tee /tmp/after.json | python3 -c 'import sys,json;d=json.load(sys.stdin);print({k:d.get(k) for k in ("ok","live_status","on_air","health","url")})'
echo '=== 6 verdict ==='
python3 - <<'PY'
import json
from pathlib import Path
def load_last(p):
t=Path(p).read_text(encoding="utf-8").strip().splitlines()[-1]
return json.loads(t)
before=load_last("/tmp/before.json")
mid=load_last("/tmp/mid.json")
after=load_last("/tmp/after.json")
end=json.loads(Path("/tmp/end_live.json").read_text(encoding="utf-8"))
print("before", before.get("live_status"), "on_air", before.get("on_air"))
print("mid", mid.get("live_status"), "on_air", mid.get("on_air"))
print("after", after.get("live_status"), "on_air", after.get("on_air"))
cdp=api=None
for s in (end.get("steps") or []):
if "cdp_end" in s: cdp=s["cdp_end"]
if "api_complete" in s: api=s["api_complete"]
print("cdp_end", json.dumps(cdp, ensure_ascii=False)[:1200] if cdp else None)
print("api_complete", api)
confirm=(cdp or {}).get("confirm_click") or {}
pre=(cdp or {}).get("pre_confirm") or {}
end_click=(cdp or {}).get("end_click") or {}
clicked_end = bool(end_click.get("ok") or pre.get("ok"))
clicked_confirm = bool(confirm.get("ok") or (pre.get("ok") and pre.get("text")))
status=str(after.get("live_status") or "")
ended = status in {"ended", "waiting"} or after.get("on_air") is False
api_ok = bool((api or {}).get("ok")) or bool((api or {}).get("completed"))
ok = clicked_confirm or ended or api_ok
print("clicked_end_stream", clicked_end)
print("clicked_confirm_End", clicked_confirm, "text", confirm.get("text") or pre.get("text"))
print("stream_ended_ui", ended)
print("SELFTEST", "PASS" if ok else "FAIL")
raise SystemExit(0 if ok else 2)
PY
'''
sftp = c.open_sftp()
with sftp.file("/tmp/_selftest_graceful_end.sh", "w") as f:
f.write(runner)
sftp.close()
print("running remote selftest...", flush=True)
_i, o, e = c.exec_command("bash /tmp/_selftest_graceful_end.sh", timeout=200)
code = o.channel.recv_exit_status()
out = (o.read() + e.read()).decode("utf-8", "replace")
print(out[-20000:] if len(out) > 20000 else out, flush=True)
c.close()
raise SystemExit(code)