# -*- coding: utf-8 -*- """Deploy yt-long quota: 14 channels x 3/day = 42 global.""" import sys import time import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") REPO = "/opt/douyinWEB" BRANCH = "test1" 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[-6000:] if len(out) > 6000 else out, flush=True) return out QUOTA_PY = r""" from app import db, yt_long_db from app.timeutil import push_date_from_ts import time db.init_db() patch = { 'yt_long_daily_max': 42, 'yt_long_channel_daily_max': 3, 'yt_long_max_concurrent': 2, 'yt_long_live_sample_batch': 2, } db.update_settings_map(patch) print('settings', {k: db.get_settings_map().get(k) for k in patch}) with db._lock: conn = db._connect() try: yt_long_db.ensure_yt_long_tables(conn) n = conn.execute( 'UPDATE yt_long_channels SET daily_max = 3, updated_at = ? WHERE daily_max < 3', (time.time(),), ).rowcount day = push_date_from_ts(time.time()) n2 = conn.execute( "UPDATE yt_long_jobs SET status='pending', error='', finished_at=0, updated_at=? " "WHERE push_date=? AND status='skipped' AND error='daily_quota_full'", (time.time(), day), ).rowcount conn.commit() print('channels_bumped', n, 'skipped_reopened', n2, 'day', day) rows = conn.execute( 'SELECT enabled, COUNT(*) c, MIN(daily_max) mn, MAX(daily_max) mx FROM yt_long_channels GROUP BY enabled' ).fetchall() for r in rows: print('ch', dict(r)) finally: conn.close() print('reader', yt_long_db.yt_long_daily_max(), yt_long_db.yt_long_channel_daily_max()) """ VERIFY_PY = r""" from app.yt_long_pipeline import yt_long_pipeline from app import yt_long_db, db st = yt_long_pipeline.status() print({k: st.get(k) for k in ['enabled','running','quota_used','quota_gap','daily_max','channel_daily_max']}) chs = yt_long_db.list_yt_long_channels() print('channels', len(chs), 'enabled', sum(1 for c in chs if c.get('enabled')), 'dmax', sorted({int(c.get('daily_max') or 0) for c in chs})) print('cfg', {k: db.get_settings_map().get(k) for k in ['yt_long_daily_max','yt_long_channel_daily_max','yt_long_max_concurrent','yt_long_live_sample_batch']}) """ def run_remote_py(code: str, label: str): import base64 b64 = base64.b64encode(code.encode("utf-8")).decode("ascii") print(f"[{label}]") return run( f"cd {REPO}/backend && echo {b64} | base64 -d > /tmp/_yt_long_quota.py " f"&& PYTHONPATH=. .venv/bin/python /tmp/_yt_long_quota.py" ) print( "[pull]", run( f"cd {REPO} && git stash push -u -m auto-quota42 || true; " f"git fetch origin {BRANCH} && git pull --ff-only origin {BRANCH} && git log --oneline -1" ), ) run_remote_py(QUOTA_PY, "quota patch") 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) run_remote_py(VERIFY_PY, "verify") print("[build]", run(f"cd {REPO}/frontend && npm run build 2>&1 | tail -n 15", timeout=600)) run("systemctl restart douyinweb-web") print("[svc]", run("systemctl is-active douyinweb-api douyinweb-web")) print( "[health]", run( "curl -sS -m 15 http://127.0.0.1:8014/api/health | " "python3 -c \"import sys,json;h=json.load(sys.stdin);yl=h.get('yt_long') or {};" "print('yt_long', yl.get('running'), yl.get('quota_used'), yl.get('quota_gap'), yl.get('daily_max'))\"" ), ) c.close() print("DONE")