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

83 lines
2.5 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import annotations
import sys
from pathlib import Path
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
LOCAL = Path(__file__).resolve().parents[1] / "backend" / "app" / "youtube_relay_pipeline.py"
REMOTE = "/opt/douyinWEB/backend/app/youtube_relay_pipeline.py"
def main() -> None:
data = LOCAL.read_bytes()
marker = b"rotate live list"
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect("107.173.30.245", 22, "root", "Xiao4669805", timeout=30, allow_agent=False, look_for_keys=False)
sftp = c.open_sftp()
try:
remote = sftp.file(REMOTE, "rb").read()
except OSError:
remote = b""
if marker not in remote:
print("uploading file…")
with sftp.file(REMOTE, "wb") as f:
f.write(data)
remote = data
else:
print("remote already has marker")
sftp.close()
print("local_bytes", len(data), "remote_has_marker", marker in remote)
def run(cmd: str, t: int = 90) -> str:
_, o, e = c.exec_command(cmd, timeout=t)
o.channel.settimeout(t)
try:
return (o.read() + e.read()).decode("utf-8", "replace")
except Exception as ex:
return f"ERR {type(ex).__name__}: {ex}"
print("active:", run("systemctl is-active douyinweb-api", 15).strip())
print("restart…")
print(run("systemctl restart douyinweb-api", 45).strip())
print("active2:", run("sleep 3; systemctl is-active douyinweb-api", 20).strip())
print(
"health:",
run(
"curl -sS -m 5 -o /dev/null -w '%{http_code}' http://127.0.0.1:8014/api/health || echo fail",
20,
).strip(),
)
print(
"state:",
run(
r"""
sleep 12
/opt/douyinWEB/backend/.venv/bin/python - <<'PY'
import json, sqlite3
con=sqlite3.connect('/opt/douyinWEB/backend/data/monitor.db')
con.row_factory=sqlite3.Row
gid='3fb8cbf7-2180-45bc-99f0-836169f0bf97'
r=dict(con.execute('select status,error,source_url,source_room_id,anchor_name,ffmpeg_pid,recent_log from yt_relay_states where group_id=?',(gid,)).fetchone() or {})
print('status', r.get('status'), 'pid', r.get('ffmpeg_pid'), 'room', r.get('source_room_id'), 'anchor', r.get('anchor_name'))
log=r.get('recent_log') or ''
try: lines=json.loads(log) if str(log).startswith('[') else str(log).splitlines()
except: lines=[]
for ln in lines[-15:]:
print(ln)
PY
""",
40,
),
)
c.close()
if __name__ == "__main__":
main()