123 lines
4.3 KiB
Python
123 lines
4.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Deploy clip-reconcile fix + immediately heal stuck scheduled-empty clips on dylive."""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import sys
|
|
import time
|
|
import http.cookiejar
|
|
import urllib.request
|
|
from pathlib import Path
|
|
|
|
import paramiko
|
|
|
|
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
ROOT = Path(r"c:\Users\admin\Desktop\douyinWEB")
|
|
FILES = [
|
|
"backend/app/db.py",
|
|
"backend/app/clip_retention.py",
|
|
]
|
|
|
|
HEAL = r'''
|
|
set -e
|
|
install -m 644 /tmp/_dy_db.py /opt/douyinWEB/backend/app/db.py
|
|
install -m 644 /tmp/_dy_ret.py /opt/douyinWEB/backend/app/clip_retention.py
|
|
# strip CRLF
|
|
python3 -c "from pathlib import Path
|
|
for p in [Path('/opt/douyinWEB/backend/app/db.py'), Path('/opt/douyinWEB/backend/app/clip_retention.py')]:
|
|
p.write_bytes(p.read_bytes().replace(b'\r\n',b'\n').replace(b'\r',b'\n'))
|
|
print('lf', p)"
|
|
|
|
systemctl restart douyinweb-api.service
|
|
sleep 5
|
|
systemctl is-active douyinweb-api.service
|
|
|
|
# Direct heal today's stuck jobs (empty clip + scheduled)
|
|
python3 <<'PY'
|
|
import sqlite3, time
|
|
conn=sqlite3.connect('/opt/douyinWEB/backend/data/monitor.db')
|
|
day=time.strftime('%Y-%m-%d', time.gmtime(time.time()+8*3600)) # rough BJ
|
|
# better BJ
|
|
import datetime
|
|
BJ=datetime.timezone(datetime.timedelta(hours=8))
|
|
day=datetime.datetime.now(BJ).strftime('%Y-%m-%d')
|
|
now=time.time()
|
|
before=conn.execute("SELECT COUNT(*) FROM shorts_jobs WHERE push_date=? AND status='scheduled' AND IFNULL(clip_path,'')='' AND IFNULL(youtube_video_id,'')=''",(day,)).fetchone()[0]
|
|
cur=conn.execute("""
|
|
UPDATE shorts_jobs
|
|
SET status='pending', clip_path='', clip_seconds=0, upload_at=0,
|
|
error='成片路径为空,已重新排队录制', finished_at=0, updated_at=?
|
|
WHERE push_date=? AND status IN ('scheduled','recorded','uploading')
|
|
AND IFNULL(youtube_video_id,'')=''
|
|
AND IFNULL(clip_path,'')=''
|
|
""",(now, day))
|
|
conn.commit()
|
|
print('day', day, 'stuck_empty_before', before, 'healed', cur.rowcount)
|
|
rows=conn.execute("SELECT status, COUNT(*) c FROM shorts_jobs WHERE push_date=? GROUP BY status",(day,)).fetchall()
|
|
print('after', {r[0]:r[1] for r in rows})
|
|
conn.close()
|
|
PY
|
|
|
|
# bump record concurrency briefly via settings if too low
|
|
python3 <<'PY'
|
|
import sqlite3
|
|
conn=sqlite3.connect('/opt/douyinWEB/backend/data/monitor.db')
|
|
for k,v in [('shorts_max_concurrent','3')]:
|
|
row=conn.execute('SELECT value FROM kv_settings WHERE key=?',(k,)).fetchone()
|
|
print('before', k, row[0] if row else None)
|
|
if row:
|
|
conn.execute('UPDATE kv_settings SET value=? WHERE key=?',(v,k))
|
|
else:
|
|
conn.execute('INSERT INTO kv_settings(key,value) VALUES(?,?)',(k,v))
|
|
conn.commit()
|
|
print('set shorts_max_concurrent=3 for catch-up')
|
|
conn.close()
|
|
PY
|
|
|
|
curl -sS -m 5 http://127.0.0.1:8014/api/ping; echo
|
|
'''
|
|
|
|
|
|
def put_host(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()
|
|
sftp.put(str(ROOT / "backend/app/db.py"), "/tmp/_dy_db.py")
|
|
sftp.put(str(ROOT / "backend/app/clip_retention.py"), "/tmp/_dy_ret.py")
|
|
with sftp.file("/tmp/_dy_heal_shorts.sh", "w") as f:
|
|
f.write(HEAL.replace("\r\n", "\n"))
|
|
sftp.close()
|
|
cmd = f"echo '{pw}' | sudo -S -p '' bash /tmp/_dy_heal_shorts.sh" if sudo else "bash /tmp/_dy_heal_shorts.sh"
|
|
_, o, e = c.exec_command(cmd, timeout=120)
|
|
print((o.read() + e.read()).decode("utf-8", "replace"))
|
|
c.close()
|
|
|
|
|
|
put_host("vps", "107.173.30.245", 22, "root", "Xiao4669805", sudo=False)
|
|
put_host("phys", "107.173.30.245", 4014, "eric", "4669805", sudo=True)
|
|
|
|
# verify public
|
|
time.sleep(4)
|
|
cj = http.cookiejar.CookieJar()
|
|
op = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
|
|
base = "https://dylive.nomadro.com"
|
|
req = urllib.request.Request(
|
|
base + "/api/auth/login",
|
|
data=json.dumps({"username": "hackrobot", "password": "Xiao4669805@"}).encode(),
|
|
method="POST",
|
|
headers={"Content-Type": "application/json"},
|
|
)
|
|
op.open(req, timeout=30)
|
|
with op.open(urllib.request.Request(base + "/api/shorts/status"), timeout=40) as r:
|
|
st = json.loads(r.read().decode())
|
|
board = st.get("board") or {}
|
|
print(
|
|
"dylive after: active_rec=", st.get("active_records"),
|
|
"active_up=", st.get("active_uploads"),
|
|
"gap=", st.get("quota_gap"),
|
|
"min_unmet=", board.get("min_unmet"),
|
|
)
|
|
print("done")
|