# -*- coding: utf-8 -*- """Safe deploy: fix X infinite repost bug.""" from __future__ import annotations import sys, time, paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") REPO = "/opt/douyinWEB" DB = f"{REPO}/backend/data/monitor.db" 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=420): _, o, e = c.exec_command(cmd, timeout=timeout) return (o.read() + e.read()).decode("utf-8", "replace").strip() before = run(f"stat -c '%s' {DB}") print("[db]", before) dirty = run(f"cd {REPO} && git diff --name-only") if dirty.strip(): print("[stash]", run(f"cd {REPO} && git stash push -m auto-before-repost-fix -- {dirty.replace(chr(10), ' ')}")) print("[pull]", run(f"cd {REPO} && git pull --ff-only origin test1 && git log --oneline -1")) run("systemctl restart douyinweb-api") for i in range(30): if run("systemctl is-active douyinweb-api", timeout=15) == "active": print("[api] active"); break time.sleep(2) # Fix currently stuck posting jobs on server DB directly print("[fix stuck]", run(f"""cd {REPO}/backend && .venv/bin/python - <<'PY' from app import db, x_clips_db as xdb db.init_db() from app.db import _connect, _lock import time with _lock: conn = _connect() try: xdb.ensure_x_clips_tables(conn) # Mark any posting jobs with finished_at>0 or x2_job_id set as posted cur = conn.execute(\"\"\" UPDATE x_clip_jobs SET status='posted', tweet_id=CASE WHEN IFNULL(tweet_id,'')='' THEN 'x2:fixed' ELSE tweet_id END, finished_at=CASE WHEN IFNULL(finished_at,0)=0 THEN ? ELSE finished_at END, updated_at=?, error='' WHERE status='posting' AND IFNULL(x2_job_id,'')!='' \"\"\", (time.time(), time.time())) print('fixed posting->posted', cur.rowcount) # Mark excessive-attempt scheduled as failed cur2 = conn.execute(\"\"\" UPDATE x_clip_jobs SET status='failed', error='excessive_retries_fixed', finished_at=?, updated_at=? WHERE status='scheduled' AND attempts >= 3 \"\"\", (time.time(), time.time())) print('fixed scheduled->failed', cur2.rowcount) conn.commit() # Show current stats rows = conn.execute("SELECT status, COUNT(*) c FROM x_clip_jobs GROUP BY status").fetchall() print('stats', {{r['status']: r['c'] for r in rows}}) finally: conn.close() PY""")) after = run(f"stat -c '%s' {DB}") print("[db]", after, "delta", int(after)-int(before) if before.isdigit() and after.isdigit() else "?") print("[svc]", run("systemctl is-active douyinweb-api douyinweb-web")) c.close() print("DONE")