# -*- coding: utf-8 -*- """Deploy zombie-session fix and reconcile on VPS.""" import json import time from pathlib import Path import paramiko HOST = "107.173.30.245" USER = "root" PASSWORD = "Xiao4669805" REMOTE = "/opt/douyinWEB" LOCAL = Path(__file__).resolve().parents[1] FILES = [ "backend/app/db.py", "backend/app/monitor.py", "backend/app/main.py", ] def main() -> None: c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect(HOST, 22, USER, PASSWORD, timeout=30, allow_agent=False, look_for_keys=False) sftp = c.open_sftp() for rel in FILES: local = LOCAL / rel remote = f"{REMOTE}/{rel}" print(f"upload {rel} -> {remote}", flush=True) sftp.put(str(local), remote) sftp.close() def run(cmd: str, timeout: int = 60) -> str: print(f"$ {cmd}", flush=True) _, o, e = c.exec_command(cmd, timeout=timeout, get_pty=True) out = o.read().decode("utf-8", errors="replace") err = e.read().decode("utf-8", errors="replace") if out.strip(): print(out.strip()[-2000:], flush=True) if err.strip(): print("ERR:", err.strip()[-500:], flush=True) return out run("systemctl restart douyinweb-api && sleep 2 && systemctl is-active douyinweb-api") for _ in range(20): _, o, _ = c.exec_command( "curl -s -o /dev/null -w '%{http_code}' http://127.0.0.1:8014/api/status", timeout=20, ) code = o.read().decode().strip() print(f"api status http={code}", flush=True) if code.startswith("2"): break time.sleep(1) _, o, _ = c.exec_command( "curl -s -X POST http://127.0.0.1:8014/api/sessions/reconcile-stale", timeout=40, get_pty=True, ) raw = o.read().decode("utf-8", errors="replace") if "{" in raw: raw = raw[raw.index("{") :] print("reconcile:", raw.strip()[:3000], flush=True) _, o, _ = c.exec_command( "curl -s http://127.0.0.1:8014/api/sessions/active", timeout=30, get_pty=True, ) raw = o.read().decode("utf-8", errors="replace") if "{" in raw: raw = raw[raw.index("{") :] data = json.loads(raw) print(f"active_total={data.get('total')}", flush=True) now = time.time() for s in data.get("sessions") or []: started = float(s.get("started_at") or 0) el = int(now - started) if started else 0 h, r = divmod(el, 3600) m, sec = divmod(r, 60) print( f" LIVE {s.get('anchor_name')} {h}:{m:02d}:{sec:02d} room={s.get('room_id')}", flush=True, ) c.close() print("done", flush=True) if __name__ == "__main__": main()