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

135 lines
5.1 KiB
Python

# -*- coding: utf-8 -*-
"""Deploy fixed confirm + retest graceful End on neko-7."""
from __future__ import annotations
import sys
from pathlib import Path
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
ROOT = Path(__file__).resolve().parents[1]
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)
sftp = c.open_sftp()
sftp.put(str(ROOT / "backend/neko/cdp_end_stream.py"), "/tmp/cdp_end_stream.py")
runner = r'''#!/bin/bash
set -euo pipefail
install -m 0644 /tmp/cdp_end_stream.py /opt/douyinWEB/backend/neko/cdp_end_stream.py
NAME=douyinweb-neko-7
NEKO_ID=7
CHANNEL_PK=82102486-f5d7-434c-b508-4c6a10c5f325
cd /opt/douyinWEB/backend
# Pause auto-relay so ffmpeg does not respawn mid-test
./.venv/bin/python - <<'PY'
from app import db
cfg=db.get_settings_map()
open("/tmp/relay_prev.json","w").write(repr({
"youtube_relay_enabled": bool(cfg.get("youtube_relay_enabled")),
"youtube_relay_timeslot_test": bool(cfg.get("youtube_relay_timeslot_test")),
}))
db.update_settings_map({"youtube_relay_enabled": False})
print("relay_paused")
PY
echo '=== BEFORE ==='
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 ("live_status","on_air","health")})'
echo '=== kill ffmpeg PIDs ==='
python3 - <<'PY'
import os, signal, subprocess, time
for _ in range(3):
out=subprocess.check_output(["ps","-eo","pid,args"], text=True, errors="replace")
n=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)
try: os.kill(pid, signal.SIGTERM); n+=1
except ProcessLookupError: pass
print("killed_batch", n)
time.sleep(1)
PY
sleep 10
echo '=== MID ==='
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 ("live_status","on_air","health")})'
echo '=== end_live_session ==='
./.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=8.0,
settle_sec=5.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)[:6000])
PY
sleep 3
echo '=== AFTER ==='
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 ("live_status","on_air","health")})'
python3 - <<'PY'
import json
from pathlib import Path
def load_last(p):
return json.loads(Path(p).read_text(encoding="utf-8").strip().splitlines()[-1])
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"))
cdp=None
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("before", before.get("live_status"), before.get("on_air"))
print("mid", mid.get("live_status"), mid.get("on_air"))
print("after", after.get("live_status"), after.get("on_air"))
print("cdp", json.dumps(cdp, ensure_ascii=False)[:1500])
print("api", api)
confirm=(cdp or {}).get("confirm_click") or {}
pre=(cdp or {}).get("pre_confirm") or {}
end_click=(cdp or {}).get("end_click") or {}
ctxt=str(confirm.get("text") or "").strip().lower()
ptxt=str(pre.get("text") or "").strip().lower()
clicked_confirm = ctxt in {"end","结束"} or ptxt in {"end","结束"}
clicked_header = bool(end_click.get("ok"))
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("completed")) or (bool((api or {}).get("ok")) and not (api or {}).get("skipped"))
# Strict: must click dialog End OR UI ended OR API completed
ok = clicked_confirm or ended or api_ok
print("clicked_header_EndStream", clicked_header)
print("clicked_dialog_End", clicked_confirm, "text", confirm.get("text") or pre.get("text"))
print("stream_ended_ui", ended)
print("SELFTEST", "PASS" if ok else "FAIL")
# restore relay flag (keep test mode as-was in prev file — only restore enabled if was on)
prev=eval(Path("/tmp/relay_prev.json").read_text())
from app import db
db.update_settings_map({"youtube_relay_enabled": bool(prev.get("youtube_relay_enabled"))})
print("relay_restored", prev.get("youtube_relay_enabled"))
raise SystemExit(0 if ok else 2)
PY
'''
with sftp.file("/tmp/_retest_end.sh", "w") as f:
f.write(runner)
sftp.close()
print("running...", flush=True)
_i, o, e = c.exec_command("bash /tmp/_retest_end.sh", timeout=240)
code = o.channel.recv_exit_status()
out = (o.read() + e.read()).decode("utf-8", "replace")
print(out[-22000:] if len(out) > 22000 else out, flush=True)
c.close()
raise SystemExit(code)