douyinWEB/scripts/deploy_pb_s3_fix.py
eric a360eec2ba Document slots/covers/PB ops UX and sync feature pass to main.
Ship zombie reconcile, S3 covers, session end/drill-down, rooms ops, and refresh README for production.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-08 20:07:15 -05:00

115 lines
3.9 KiB
Python

# -*- coding: utf-8 -*-
"""Deploy PB+S3 architecture fix and sync data."""
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/pb_sync.py",
"backend/app/cover.py",
"backend/app/db.py",
"backend/app/main.py",
"backend/app/monitor.py",
"backend/app/douyin.py",
]
def run(c, cmd: str, timeout: int = 300) -> tuple[int, str]:
print("$", cmd[:120], flush=True)
chan = c.get_transport().open_session()
chan.settimeout(timeout)
chan.get_pty()
chan.exec_command(cmd)
buf = []
while True:
if chan.recv_ready():
buf.append(chan.recv(65535).decode("utf-8", "replace"))
if chan.exit_status_ready() and not chan.recv_ready():
break
time.sleep(0.2)
while chan.recv_ready():
buf.append(chan.recv(65535).decode("utf-8", "replace"))
code = chan.recv_exit_status()
out = "".join(buf)
print(out[-2500:], flush=True)
return code, out
def curl_json(c, path: str, method: str = "GET", timeout: int = 300) -> dict:
flag = "-X POST" if method == "POST" else ""
_, out = run(c, f"curl -s {flag} 'http://127.0.0.1:8014{path}'", timeout=timeout)
if "{" not in out:
return {"raw": out[-500:]}
raw = out[out.index("{") :]
# strip trailing junk
try:
return json.loads(raw)
except Exception:
# try truncate to last }
end = raw.rfind("}")
if end > 0:
try:
return json.loads(raw[: end + 1])
except Exception:
pass
return {"raw": raw[:800]}
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:
print("upload", rel, flush=True)
sftp.put(str(LOCAL / rel), f"{REMOTE}/{rel}")
sftp.close()
run(c, "systemctl restart douyinweb-api --no-block; sleep 4; systemctl is-active douyinweb-api")
for _ in range(20):
_, out = run(c, "curl -s -o /dev/null -w '%{http_code}' http://127.0.0.1:8014/api/health", timeout=20)
if "200" in out:
break
time.sleep(1)
print("=== PB bootstrap ===", flush=True)
print(curl_json(c, "/api/pb/bootstrap", "POST"), flush=True)
print("=== recapture active covers -> S3 ===", flush=True)
print(curl_json(c, "/api/sessions/active/recapture-covers", "POST", timeout=300), flush=True)
print("=== sync rooms to PB ===", flush=True)
print(curl_json(c, "/api/pb/sync-rooms", "POST", timeout=180), flush=True)
print("=== sync sessions to PB ===", flush=True)
print(curl_json(c, "/api/pb/sync-sessions?limit=500", "POST", timeout=180), flush=True)
print("=== verify sample ===", flush=True)
run(
c,
"/opt/douyinWEB/backend/.venv/bin/python - <<'PY'\n"
"import sqlite3, json, urllib.request\n"
"c=sqlite3.connect('/opt/douyinWEB/backend/data/monitor.db'); c.row_factory=sqlite3.Row\n"
"total=c.execute('select count(*) from sessions').fetchone()[0]\n"
"s3=c.execute(\"select count(*) from sessions where cover_path like 'http%'\").fetchone()[0]\n"
"empty=c.execute(\"select count(*) from sessions where cover_path is null or trim(cover_path)=''\").fetchone()[0]\n"
"print(f'sessions={total} s3_covers={s3} no_cover={empty}')\n"
"row=c.execute(\"select id,anchor_name,substr(cover_path,1,90) from sessions where cover_path like 'http%' order by started_at desc limit 1\").fetchone()\n"
"print('sample', dict(row) if row else None)\n"
"PY",
timeout=40,
)
c.close()
print("DONE", flush=True)
if __name__ == "__main__":
main()