douyinWEB/scripts/_trim_yt_long_quota.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

85 lines
3.0 KiB
Python

# -*- coding: utf-8 -*-
import sys
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
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=120):
_, 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
py = r'''
from app import db, yt_long_db
from app.yt_long_db import _connect, _lock, _QUOTA_STATUSES
from collections import Counter
import time
db.init_db()
cfg = db.get_settings_map()
day = yt_long_db.yt_long_stats().get("push_date")
dmax = yt_long_db.yt_long_daily_max(cfg)
print("day", day, "daily_max", dmax)
keep_status = ("uploaded", "uploading", "scheduled", "recording", "recorded")
with _lock:
conn = _connect()
try:
rows = conn.execute(
"""
SELECT id, status, created_at, anchor_name FROM yt_long_jobs
WHERE push_date = ? AND status IN (%s)
ORDER BY
CASE status
WHEN 'uploaded' THEN 0
WHEN 'uploading' THEN 1
WHEN 'scheduled' THEN 2
WHEN 'recorded' THEN 3
WHEN 'recording' THEN 4
WHEN 'pending' THEN 5
ELSE 9
END,
created_at ASC
""" % (",".join("?" for _ in _QUOTA_STATUSES)),
(day, *_QUOTA_STATUSES),
).fetchall()
print("quota rows", len(rows))
keep_ids = set()
skip_ids = []
for i, r in enumerate(rows):
if i < dmax:
keep_ids.add(str(r["id"]))
print(" KEEP", r["status"], (r["anchor_name"] or "")[:20])
else:
# only skip pending / recording orphans that aren't actively needed
if str(r["status"]) == "pending":
skip_ids.append(str(r["id"]))
print(" SKIP", (r["anchor_name"] or "")[:20])
else:
keep_ids.add(str(r["id"]))
print(" KEEP(over,active)", r["status"], (r["anchor_name"] or "")[:20])
now = time.time()
for jid in skip_ids:
conn.execute(
"UPDATE yt_long_jobs SET status='skipped', error='daily_quota_overflow', finished_at=?, updated_at=? WHERE id=? AND status='pending'",
(now, now, jid),
)
conn.commit()
print("skipped", len(skip_ids))
finally:
conn.close()
jobs = yt_long_db.list_yt_long_jobs(limit=40)
print(dict(Counter(j.get("status") for j in jobs)))
print("used", yt_long_db.yt_long_quota_used(day), "gap", yt_long_db.yt_long_quota_gap(day, cfg))
'''
sftp = c.open_sftp()
with sftp.file("/tmp/yt_long_trim.py", "w") as f:
f.write(py)
sftp.close()
print(run("cd /opt/douyinWEB/backend && PYTHONPATH=. .venv/bin/python /tmp/yt_long_trim.py"))
c.close()