douyinWEB/scripts/_fix_yt_long_quota_both.py
eric ca7724d3e4 Fix yt-long master quota: enabled channels only, stop Shorts×9 inflation.
Mother-clip daily max now tracks enabled oauth long channels (7×2=14), not Shorts fleet size; UI upload card ignores disabled Account B rows.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-16 21:11:23 -05:00

117 lines
4.1 KiB
Python

# -*- coding: utf-8 -*-
"""Fix yt-long quota inflation on dy + dylive and restart."""
from __future__ import annotations
import sys
import time
from pathlib import Path
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
ROOT = Path(__file__).resolve().parents[1]
PASS = "Xiao4669805"
JUMP = "107.173.30.245"
DY = "108.174.50.68"
FILES = [
"backend/app/clip_cascade.py",
"backend/app/yt_long_db.py",
"backend/app/main.py",
"frontend/src/app/yt-long/page.tsx",
]
SH = r'''
set -e
cp -a /tmp/_ql_clip_cascade.py /opt/douyinWEB/backend/app/clip_cascade.py
cp -a /tmp/_ql_yt_long_db.py /opt/douyinWEB/backend/app/yt_long_db.py
cp -a /tmp/_ql_main.py /opt/douyinWEB/backend/app/main.py
install -D -m 644 /tmp/_ql_page.tsx /opt/douyinWEB/frontend/src/app/yt-long/page.tsx
cd /opt/douyinWEB/backend
.venv/bin/python -m py_compile app/clip_cascade.py app/yt_long_db.py app/main.py
.venv/bin/python - <<'PY'
from app import db, yt_long_db
from app.clip_cascade import estimate_platform_daily_need, ensure_source_covers_platforms
print("reconcile", yt_long_db.reconcile_yt_long_upload_caps())
print("need", estimate_platform_daily_need())
print("adj", ensure_source_covers_platforms())
cfg = db.get_settings_map()
en = [c for c in yt_long_db.list_yt_long_channels(enabled=True) if c.get("enabled") and str(c.get("refresh_token") or "").strip()]
print("enabled_oauth", len(en))
print("caps", yt_long_db.yt_long_enabled_upload_cap(), cfg.get("yt_long_daily_max"), cfg.get("yt_long_source_daily_max"))
print("source_fn", yt_long_db.yt_long_source_daily_max(), "used", yt_long_db.yt_long_source_used(), "gap", yt_long_db.yt_long_source_gap())
print("upload_gap", yt_long_db.yt_long_upload_gap())
PY
systemctl restart douyinweb-api
for i in $(seq 1 20); do
c=$(curl -sS -m 3 -o /dev/null -w "%{http_code}" http://127.0.0.1:8014/api/health || echo x)
echo health:$c
[ "$c" = "200" ] && break
sleep 2
done
cd /opt/douyinWEB/frontend && npm run build 2>&1 | tail -20
systemctl restart douyinweb-web
sleep 2
systemctl is-active douyinweb-api douyinweb-web
echo HOST_DONE
'''
def wait_out(c: paramiko.SSHClient, path: str, token: str = "HOST_DONE", rounds: int = 150) -> str:
out = ""
for i in range(rounds):
time.sleep(3)
_, o, _ = c.exec_command(f"tail -c 25000 {path} 2>/dev/null", timeout=20)
out = o.read().decode("utf-8", "replace")
if token in out and "EXIT:" in out:
return out
if i % 5 == 0:
print("poll", i, out[-140:].replace("\n", " | "), flush=True)
return out
def main() -> int:
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(JUMP, 22, "root", PASS, timeout=40, allow_agent=False, look_for_keys=False)
sftp = c.open_sftp()
mapping = {
"clip_cascade.py": "/tmp/_ql_clip_cascade.py",
"yt_long_db.py": "/tmp/_ql_yt_long_db.py",
"main.py": "/tmp/_ql_main.py",
"page.tsx": "/tmp/_ql_page.tsx",
}
for rel in FILES:
name = Path(rel).name
data = (ROOT / rel).read_bytes().replace(b"\r\n", b"\n")
with sftp.file(mapping[name], "wb") as f:
f.write(data)
print("staged", name, len(data), flush=True)
with sftp.file("/tmp/_ql_remote.sh", "w") as f:
f.write(SH)
sftp.close()
print("\n===== dylive =====", flush=True)
c.exec_command(
"bash /tmp/_ql_remote.sh > /tmp/_ql_dylive.out 2>&1; echo EXIT:$? >> /tmp/_ql_dylive.out",
timeout=1,
)
print(wait_out(c, "/tmp/_ql_dylive.out")[-12000:], flush=True)
print("\n===== dy.live =====", flush=True)
files = " ".join(mapping.values()) + " /tmp/_ql_remote.sh"
c.exec_command(
f"SSHPASS={PASS} sshpass -e scp -o StrictHostKeyChecking=no {files} root@{DY}:/tmp/ && "
f"SSHPASS={PASS} sshpass -e ssh -o StrictHostKeyChecking=no root@{DY} "
f"'bash /tmp/_ql_remote.sh' > /tmp/_ql_dy.out 2>&1; echo EXIT:$? >> /tmp/_ql_dy.out",
timeout=1,
)
print(wait_out(c, "/tmp/_ql_dy.out")[-12000:], flush=True)
c.close()
return 0
if __name__ == "__main__":
raise SystemExit(main())