douyinWEB/scripts/_deploy_exclusive_probe_rotate.py
eric c1f3bd3c57 Fix exclusive sticky when Douyin probe says offline (status=4).
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-14 06:03:46 -05:00

100 lines
3.6 KiB
Python

# -*- coding: utf-8 -*-
"""Deploy exclusive probe-offline rotate fix; keep current push if healthy."""
from __future__ import annotations
import sys
import time
from pathlib import Path
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
ROOT = Path(__file__).resolve().parents[1]
PASS = "Xiao4669805"
HOSTS = [("dylive", "107.173.30.245"), ("dy", "108.174.50.68")]
FILES = [
("backend/app/youtube_relay_pipeline.py", "/opt/douyinWEB/backend/app/youtube_relay_pipeline.py"),
("backend/tests/test_relay_normal_auto.py", "/opt/douyinWEB/backend/tests/test_relay_normal_auto.py"),
]
REMOTE = r'''
set -e
cd /opt/douyinWEB/backend
.venv/bin/python -m pytest tests/test_relay_normal_auto.py -q --tb=line || true
systemctl restart douyinweb-api
for i in $(seq 1 25); do
c=$(curl -sS -m 3 -o /dev/null -w '%{http_code}' http://127.0.0.1:8014/api/health || echo x)
echo health_$i=$c
[ "$c" = "200" ] && break
sleep 2
done
PYTHONPATH=/opt/douyinWEB/backend .venv/bin/python - <<'PY'
import json, time
from app import youtube_relay_db, yt_assign_db
from app.youtube_neko import _cdp_studio_page_signals
from app.youtube_relay_pipeline import youtube_relay_pipeline as rp
# walking tour on dylive
groups = {str(g.get("name")): g for g in yt_assign_db.list_groups(include_urls=False)}
for name in ("walking tour", "Nomadro Hub"):
g = groups.get(name)
if not g:
print("missing", name); continue
st = youtube_relay_db.get_relay_state(g["id"]) or {}
print(name, "status", st.get("status"), "src", st.get("source_room_id"), "pid", st.get("ffmpeg_pid"), "err", str(st.get("error") or "")[:80])
print("exclusive_winner", getattr(rp, "_exclusive_winner", None))
# give worker a tick
time.sleep(8)
for name in ("walking tour",):
g = groups.get(name)
if not g: continue
st = youtube_relay_db.get_relay_state(g["id"]) or {}
print("after_tick", name, st.get("status"), st.get("ffmpeg_pid"), (st.get("recent_log") or [])[-2:])
nid = int(g.get("neko_instance_id") or 0)
if nid:
try:
sig = _cdp_studio_page_signals(nid, force=True)
print("studio", sig.get("live_status"), "on_air", sig.get("on_air"), "recv", sig.get("encoder_receiving"))
except Exception as e:
print("studio_err", e)
PY
systemctl is-active douyinweb-api
'''
def connect(host: str):
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
for i in range(8):
try:
c.connect(host, 22, "root", PASS, timeout=40, allow_agent=False, look_for_keys=False, banner_timeout=90)
return c
except Exception as e:
print("ssh_retry", host, i, e, flush=True)
time.sleep(2 + i)
raise SystemExit(f"ssh_fail {host}")
def main():
for label, host in HOSTS:
print(f"\n===== {label} {host} =====", flush=True)
c = connect(host)
sftp = c.open_sftp()
for rel, dst in FILES:
data = (ROOT / rel).read_bytes().replace(b"\r\n", b"\n").replace(b"\r", b"\n")
tmp = f"/tmp/_excl_{Path(rel).name}"
with sftp.file(tmp, "wb") as f:
f.write(data)
_, o, e = c.exec_command(f"cat {tmp} > {dst} && echo OK {dst}", timeout=30)
print((o.read() + e.read()).decode("utf-8", "replace")[:200], flush=True)
with sftp.file("/tmp/_deploy_excl_rotate.sh", "w") as f:
f.write(REMOTE.replace("\r\n", "\n"))
sftp.close()
_, o, e = c.exec_command("bash /tmp/_deploy_excl_rotate.sh", timeout=240)
print((o.read() + e.read()).decode("utf-8", "replace")[-5000:], flush=True)
c.close()
if __name__ == "__main__":
main()