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

86 lines
2.6 KiB
Python

#!/usr/bin/env python3
"""Deploy live-map overcount fix (edge_assign_live sticky 在播)."""
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]
def ssh():
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,
)
return c
def run(c, cmd, t=300):
print(f"\n$ {cmd[:220]}", flush=True)
_i, o, e = c.exec_command(cmd, timeout=t)
code = o.channel.recv_exit_status()
out = (o.read() + e.read()).decode("utf-8", "replace")
print(out[-16000:] if len(out) > 16000 else out, flush=True)
return code, out
c = ssh()
sftp = c.open_sftp()
sftp.put(str(ROOT / "backend/app/youtube_live.py"), "/tmp/youtube_live.py")
sftp.put(str(ROOT / "backend/tests/test_youtube_live.py"), "/tmp/test_youtube_live.py")
sftp.close()
code, _ = run(
c,
r"""
set -euo pipefail
install -m 0644 /tmp/youtube_live.py /opt/douyinWEB/backend/app/youtube_live.py
install -m 0644 /tmp/test_youtube_live.py /opt/douyinWEB/backend/tests/test_youtube_live.py
cd /opt/douyinWEB/backend
./.venv/bin/python -m py_compile app/youtube_live.py
./.venv/bin/python -m pytest -q tests/test_youtube_live.py
systemctl restart douyinweb-api.service
sleep 4
systemctl is-active douyinweb-api.service
./.venv/bin/python - <<'PY'
import asyncio, json, time
from app import youtube_live
from app.youtube_relay_pipeline import youtube_relay_pipeline as pipe
m = asyncio.run(youtube_live.build_live_map(force=True))
print("live_count", m.get("live_count"), "age", round(time.time()-float(m.get("checked_at") or 0),1))
lives=[]
for k,v in (m.get("entries") or {}).items():
if not (k.startswith("assign:") and v.get("is_live")):
continue
st=v.get("studio") if isinstance(v.get("studio"), dict) else {}
lives.append({
"key": k[:40],
"title": v.get("title"),
"source": v.get("source"),
"studio_src": (st or {}).get("detect_source"),
"on_air": (st or {}).get("on_air"),
"status": (st or {}).get("live_status"),
})
print(json.dumps(lives, ensure_ascii=False, indent=2))
st=pipe.status()
push=[(r.get("name"), r.get("business_status"), r.get("is_pushing")) for r in (st.get("groups") or []) if r.get("is_pushing") or r.get("business_status") in {"streaming","relay_slow"}]
print("pushing", push)
PY
""",
)
c.close()
raise SystemExit(code)