douyinWEB/scripts/_deploy_yt_long_fix.py
eric d409e416da Ship physical dual-edge stack, Studio CDP live metrics, and ops tooling.
Capture Neko/WG/network hubs, Studio viewer charts, physical deploy docs, and one-shot scripts so VPS vs physical can be toggled later without losing WIP.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-04 22:42:40 -05:00

124 lines
4.0 KiB
Python

# -*- coding: utf-8 -*-
"""Deploy yt-long concurrency/quota fix and clean orphan recordings."""
import sys
import time
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
REPO = "/opt/douyinWEB"
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)
def run(cmd, timeout=600):
_, o, e = c.exec_command(cmd, timeout=timeout)
out = (o.read() + e.read()).decode("utf-8", "replace").strip()
print(out[-5000:] if len(out) > 5000 else out, flush=True)
return out
cleanup_py = r'''
from app import db, yt_long_db
from app.yt_long_db import _connect, _lock
from collections import Counter
import time, os, subprocess
db.init_db()
alive = set()
try:
out = subprocess.check_output("ps aux | grep '[f]fmpeg' | grep yt_long || true", shell=True, text=True)
for line in out.splitlines():
for part in line.split():
if part.endswith(".mp4") and "yt_long" in part:
alive.add(os.path.basename(part).replace(".mp4", ""))
except Exception as e:
print("ps err", e)
print("alive_ffmpeg_jobs", alive)
with _lock:
conn = _connect()
try:
now = time.time()
rows = conn.execute(
"SELECT id,status,anchor_name,started_at,updated_at FROM yt_long_jobs WHERE status='recording'"
).fetchall()
reset = 0
keep = 0
for r in rows:
jid = str(r["id"])
if jid in alive:
keep += 1
print(" keep recording", jid[:8], r["anchor_name"])
continue
conn.execute(
"UPDATE yt_long_jobs SET status='pending', started_at=0, error='orphan_recording_reset', updated_at=? WHERE id=?",
(now, jid),
)
reset += 1
print(" reset", jid[:8], r["anchor_name"])
cur = conn.execute(
"""
UPDATE yt_long_jobs SET status='scheduled', error='upload_stale_reclaim', updated_at=?
WHERE status='uploading' AND IFNULL(youtube_video_id,'')='' AND updated_at < ?
""",
(now, now - 600),
)
print("upload reclaim", cur.rowcount)
conn.commit()
print("reset_recording", reset, "kept", keep)
finally:
conn.close()
jobs = yt_long_db.list_yt_long_jobs(limit=40)
print("status", dict(Counter(j.get("status") for j in jobs)))
cfg = db.get_settings_map()
print(
"quota_used",
yt_long_db.yt_long_quota_used(),
"gap",
yt_long_db.yt_long_quota_gap(cfg=cfg),
"daily_max",
yt_long_db.yt_long_daily_max(cfg),
)
'''
after_py = r'''
from app import db, yt_long_db
from collections import Counter
db.init_db()
jobs = yt_long_db.list_yt_long_jobs(limit=40)
print(dict(Counter(j.get("status") for j in jobs)))
for j in jobs[:12]:
print(j.get("status"), (j.get("anchor_name") or "")[:18], (j.get("error") or "")[:40])
'''
sftp = c.open_sftp()
with sftp.file("/tmp/yt_long_cleanup.py", "w") as f:
f.write(cleanup_py)
with sftp.file("/tmp/yt_long_after.py", "w") as f:
f.write(after_py)
sftp.close()
print("[pull]", run(f"cd {REPO} && git stash push -u -m auto-yt-long-fix || true; git pull --ff-only origin test1 && git log --oneline -1"))
print("[api restart]")
run("systemctl restart douyinweb-api")
for _ in range(40):
if run("systemctl is-active douyinweb-api", timeout=15) == "active":
print("[api] active")
break
time.sleep(2)
time.sleep(3)
print("[cleanup]")
print(run("cd /opt/douyinWEB/backend && .venv/bin/python /tmp/yt_long_cleanup.py"))
print("[build]", run(f"cd {REPO}/frontend && npm run build 2>&1 | tail -n 15", timeout=600))
run("systemctl restart douyinweb-web")
time.sleep(2)
print("[svc]", run("systemctl is-active douyinweb-api douyinweb-web"))
time.sleep(8)
print("[after]")
print(run("cd /opt/douyinWEB/backend && .venv/bin/python /tmp/yt_long_after.py"))
print("[ffmpeg]", run("ps aux | grep '[f]fmpeg' | grep yt_long | wc -l"))
c.close()
print("DONE")