douyinWEB/scripts/_verify_excl_and_deploy_dy.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

86 lines
3.3 KiB
Python

# -*- coding: utf-8 -*-
import sys
import time
from pathlib import Path
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
PASS = "Xiao4669805"
def probe(host: str, label: str):
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(host, 22, "root", PASS, timeout=50, allow_agent=False, look_for_keys=False, banner_timeout=120)
cmd = r'''
cd /opt/douyinWEB/backend && PYTHONPATH=/opt/douyinWEB/backend .venv/bin/python - <<'PY'
import json, subprocess
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, _claim_first_live_normal, _normal_auto_candidates
groups = yt_assign_db.list_groups(include_urls=True)
by = {g.get("name"): g for g in groups}
g = by.get("walking tour")
st = youtube_relay_db.get_relay_state(g["id"]) if g else {}
print("wt", (st or {}).get("status"), "pid", (st or {}).get("ffmpeg_pid"), "src", (st or {}).get("source_room_id"), "err", str((st or {}).get("error") or "")[:100])
print("log", ((st or {}).get("recent_log") or [])[-4:])
print("winner", getattr(rp, "_exclusive_winner", None))
claimed = _claim_first_live_normal(_normal_auto_candidates(groups))
print("claim", claimed[1].get("name") if claimed else None, claimed[2] if claimed else None)
print("ffmpeg", subprocess.getoutput("ps -ef | grep 'ffmpeg' | grep -v grep | wc -l").strip())
if g:
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"), "lost", sig.get("encoder_lost"))
except Exception as e:
print("studio_err", e)
PY
'''
_, o, e = c.exec_command(cmd, timeout=60)
print(f"=== {label} ===")
print((o.read() + e.read()).decode("utf-8", "replace"))
c.close()
def deploy_dy():
root = Path(__file__).resolve().parents[1]
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
for i in range(10):
try:
c.connect("108.174.50.68", 22, "root", PASS, timeout=50, allow_agent=False, look_for_keys=False, banner_timeout=120)
break
except Exception as e:
print("dy_ssh", i, e, flush=True)
time.sleep(3 + i)
else:
print("dy_ssh_fail")
return
sftp = c.open_sftp()
for rel, dst in [
("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"),
]:
data = (root / rel).read_bytes().replace(b"\r\n", b"\n")
tmp = f"/tmp/_excl_{Path(rel).name}"
with sftp.file(tmp, "wb") as f:
f.write(data)
c.exec_command(f"cat {tmp} > {dst}", timeout=20)
sftp.close()
_, o, e = c.exec_command(
"systemctl restart douyinweb-api; sleep 4; curl -sS -m 5 -o /dev/null -w '%{http_code}' http://127.0.0.1:8014/api/health; echo; systemctl is-active douyinweb-api",
timeout=60,
)
print("dy_deploy", (o.read() + e.read()).decode("utf-8", "replace"))
c.close()
if __name__ == "__main__":
time.sleep(20)
probe("107.173.30.245", "dylive")
deploy_dy()