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

93 lines
3.1 KiB
Python

# -*- coding: utf-8 -*-
"""Hotfix pull-forward packing + verify due slots."""
from __future__ import annotations
import sys
from pathlib import Path
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
ROOT = Path(r"c:\Users\admin\Desktop\douyinWEB")
LOCAL = ROOT / "backend/app/shorts_schedule.py"
SCRIPT = r'''#!/bin/bash
set -euo pipefail
python3 - <<'PY'
from pathlib import Path
src=Path('/tmp/_dy_shorts_schedule.py')
dst=Path('/opt/douyinWEB/backend/app/shorts_schedule.py')
dst.write_bytes(src.read_bytes().replace(b'\r\n',b'\n').replace(b'\r',b'\n'))
print('ok', dst.stat().st_size)
PY
cd /opt/douyinWEB
. backend/.venv/bin/activate
python -m py_compile backend/app/shorts_schedule.py
systemctl restart douyinweb-api.service
sleep 5
systemctl is-active douyinweb-api.service
cd backend
python <<'PY'
import sys, sqlite3, time, datetime
sys.path.insert(0, "/opt/douyinWEB/backend")
from app.shorts_schedule import fleet_pace_plan, pull_forward_due_uploads
print("pace", fleet_pace_plan())
print("pulled", pull_forward_due_uploads())
BJ = datetime.timezone(datetime.timedelta(hours=8))
now = time.time()
day = datetime.datetime.now(BJ).strftime("%Y-%m-%d")
con = sqlite3.connect("/opt/douyinWEB/backend/data/monitor.db")
due = fut = 0
hours = {}
q = (
"SELECT upload_at FROM shorts_jobs WHERE push_date=? "
"AND status IN ('scheduled','recorded') AND IFNULL(clip_path,'')!=''"
)
for (ua0,) in con.execute(q, (day,)):
ua = float(ua0 or 0)
if ua <= now + 90:
due += 1
else:
fut += 1
if ua > 0:
h = datetime.datetime.fromtimestamp(ua, BJ).hour
hours[h] = hours.get(h, 0) + 1
print("due_now", due, "future", fut, "by_hour", dict(sorted(hours.items())))
# next 8
print("next:")
for r in con.execute(
"SELECT upload_at, status, substr(channel_pk,1,8) FROM shorts_jobs "
"WHERE push_date=? AND status IN ('scheduled','recorded') AND IFNULL(clip_path,'')!='' "
"ORDER BY CASE WHEN upload_at<=0 THEN 1e18 ELSE upload_at END LIMIT 8",
(day,),
):
ua = float(r[0] or 0)
bj = datetime.datetime.fromtimestamp(ua, BJ).strftime("%H:%M:%S") if ua else "-"
print(bj, "in", int(ua - now) if ua else "?", "s", r[1], r[2])
con.close()
PY
'''
def run(label, host, port, user, pw, sudo=False):
print(f"\n===== {label} =====")
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(host, port, user, pw, timeout=45, allow_agent=False, look_for_keys=False)
sftp = c.open_sftp()
data = LOCAL.read_bytes().replace(b"\r\n", b"\n").replace(b"\r", b"\n")
with sftp.file("/tmp/_dy_shorts_schedule.py", "wb") as f:
f.write(data)
with sftp.file("/tmp/_dy_pace_pull.sh", "w") as f:
f.write(SCRIPT.replace("\r\n", "\n"))
sftp.close()
cmd = f"echo '{pw}' | sudo -S -p '' bash /tmp/_dy_pace_pull.sh" if sudo else "bash /tmp/_dy_pace_pull.sh"
_, o, e = c.exec_command(cmd, timeout=180)
print((o.read() + e.read()).decode("utf-8", "replace"))
c.close()
run("vps-dylive", "107.173.30.245", 22, "root", "Xiao4669805", sudo=False)
run("phys-dy", "107.173.30.245", 4014, "eric", "4669805", sudo=True)
print("DONE")