# -*- coding: utf-8 -*- """Align dy.nomadro.live (108 VPS) with stable dylive harden + clip CPU guard.""" from __future__ import annotations import sys import time from pathlib import Path import httpx import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") ROOT = Path(__file__).resolve().parents[1] JUMP = "107.173.30.245" DY = "108.174.50.68" PASS = "Xiao4669805" FILES = [ "backend/app/host_metrics.py", "backend/app/main.py", "backend/app/yt_long_pipeline.py", "backend/app/shorts_pipeline.py", "backend/app/tiktok_pipeline.py", "backend/app/x_clips_pipeline.py", "backend/app/procutil.py", "backend/app/youtube_relay.py", "backend/app/yt_long_clip.py", "backend/app/shorts_clip.py", "backend/app/clip_cascade.py", "scripts/linux/api_health_watch.sh", ] # Match dylive tightness but scaled for 8G (dylive=700/900 on 4G). OOM_CONF = """[Service] MemoryAccounting=yes # Python-only after ffmpeg is in douyinweb-ffmpeg.slice (dylive pattern). MemoryHigh=1100M MemoryMax=1500M MemorySwapMax=256M OOMPolicy=kill OOMScoreAdjust=400 """ OVERRIDE = """[Service] TimeoutStartSec=180 TimeoutStopSec=15 KillMode=control-group Environment=PYTHONUNBUFFERED=1 """ PUBLIC_BASE = """[Service] Environment=DY_PUBLIC_BASE_URL=https://dy.nomadro.live Environment=DY_INSTALL_SHAPE=vps Environment=DY_NEKO_HTTP_BIND=127.0.0.1 """ CPU_API = """[Service] Nice=5 CPUWeight=100 """ CPU_WEB = """[Service] Nice=10 CPUWeight=40 """ FFMPEG_SLICE = """[Unit] Description=douyinWEB ffmpeg memory slice (outside API cgroup) Before=douyinweb-api.service [Slice] MemoryAccounting=yes MemoryHigh=1800M MemoryMax=2800M MemorySwapMax=512M CPUAccounting=yes # Cap encode so live+clip cannot pin all cores (4c host → leave ~2 for API/Neko). CPUQuota=200% CPUWeight=40 [Install] WantedBy=multi-user.target """ API_WATCH = """[Unit] Description=douyinWEB API health hang watchdog After=network-online.target douyinweb-api.service Wants=network-online.target [Service] Type=simple Environment=DY_API_WATCH_INTERVAL=15 Environment=DY_API_WATCH_FAILS=3 Environment=DY_API_WATCH_COOLDOWN=90 Environment=DY_API_WATCH_START_GRACE=90 Environment=DY_API_WATCH_RECVQ_LIMIT=80 ExecStart=/bin/bash /opt/douyinWEB/scripts/linux/api_health_watch.sh Restart=always RestartSec=5 [Install] WantedBy=multi-user.target """ REMOTE = r''' set -e TS=$(date +%s) install -d -m 755 /opt/douyinWEB/scripts/linux install -d -m 755 /etc/systemd/system/douyinweb-api.service.d install -d -m 755 /etc/systemd/system/douyinweb-web.service.d for f in host_metrics.py main.py yt_long_pipeline.py shorts_pipeline.py tiktok_pipeline.py x_clips_pipeline.py procutil.py youtube_relay.py yt_long_clip.py shorts_clip.py clip_cascade.py; do [ -f /tmp/_align_$f ] || continue cp -a /opt/douyinWEB/backend/app/$f /opt/douyinWEB/backend/app/${f}.bak-align-${TS} 2>/dev/null || true cp -a /tmp/_align_$f /opt/douyinWEB/backend/app/$f done install -m 755 /tmp/_align_api_health_watch.sh /opt/douyinWEB/scripts/linux/api_health_watch.sh install -m 644 /tmp/_align_ffmpeg.slice /etc/systemd/system/douyinweb-ffmpeg.slice install -m 644 /tmp/_align_api_watch.service /etc/systemd/system/douyinweb-api-watch.service install -m 644 /tmp/_align_oom.conf /etc/systemd/system/douyinweb-api.service.d/oom-protect-chrome.conf install -m 644 /tmp/_align_override.conf /etc/systemd/system/douyinweb-api.service.d/override.conf install -m 644 /tmp/_align_public.conf /etc/systemd/system/douyinweb-api.service.d/public-base.conf install -m 644 /tmp/_align_cpu_api.conf /etc/systemd/system/douyinweb-api.service.d/cpu.conf install -m 644 /tmp/_align_cpu_web.conf /etc/systemd/system/douyinweb-web.service.d/cpu.conf cd /opt/douyinWEB/backend .venv/bin/python -m py_compile app/host_metrics.py app/main.py app/yt_long_pipeline.py app/shorts_pipeline.py app/tiktok_pipeline.py app/x_clips_pipeline.py echo COMPILE_OK grep -n "clip_encode_defer_reason\|Instant liveness" app/host_metrics.py app/main.py app/yt_long_pipeline.py | head -20 .venv/bin/python - <<'PY' from app import db patch = { "yt_long_max_concurrent": 1, "shorts_max_concurrent": 1, "shorts_upload_max_concurrent": 1, "tiktok_max_concurrent": 1, "x_clips_max_concurrent": 1, "youtube_relay_quality_preset": "vps_ultra", } out = db.update_settings_map(patch, audit_actor="align_dylive") print("settings", {k: (out or {}).get(k) for k in patch}) PY # If a clip encode is fighting live push right now, stop clip-only ffmpeg (keep rtmps youtube). echo "=== reclaim clip ffmpeg if live push ===" if pgrep -f 'ffmpeg.*rtmps?://.*(youtube|youtu)' >/dev/null 2>&1; then echo live_push_present # kill non-push encodes (mp4 output / clips paths) only pkill -9 -f 'ffmpeg.*(clips/yt_long|clips/shorts|/data/clips/)' 2>/dev/null || true fi systemctl daemon-reload systemctl enable douyinweb-ffmpeg.slice douyinweb-api-watch.service systemctl start douyinweb-ffmpeg.slice systemctl restart douyinweb-api sleep 5 for i in $(seq 1 25); do code=$(curl -sS -m 3 -o /dev/null -w "%{http_code}" http://127.0.0.1:8014/api/health || echo x) echo health_$i:$code [ "$code" = "200" ] && break sleep 2 done systemctl restart douyinweb-api-watch sleep 1 systemctl is-active douyinweb-api douyinweb-api-watch douyinweb-ffmpeg.slice douyinweb-web echo MEM: $(systemctl show douyinweb-api -p MemoryHigh,MemoryMax,MemoryCurrent --value | tr '\n' ' ') echo SLICE_CPU: $(systemctl show douyinweb-ffmpeg.slice -p CPUQuota --value) curl -sS -m 3 -o /dev/null -w "health_t=%{time_total} code=%{http_code}\n" http://127.0.0.1:8014/api/health uptime ps -eo pid,pcpu,cgroup:40,cmd | grep '[f]fmpeg' | head -8 || true echo DY_ALIGN_DONE ''' def main() -> int: c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect(JUMP, 22, "root", PASS, timeout=40, allow_agent=False, look_for_keys=False) sftp = c.open_sftp() for rel in FILES: name = Path(rel).name data = (ROOT / rel).read_bytes().replace(b"\r\n", b"\n") with sftp.file(f"/tmp/_align_{name}", "wb") as f: f.write(data) print("staged", name, len(data), flush=True) with sftp.file("/tmp/_align_oom.conf", "w") as f: f.write(OOM_CONF) with sftp.file("/tmp/_align_override.conf", "w") as f: f.write(OVERRIDE) with sftp.file("/tmp/_align_public.conf", "w") as f: f.write(PUBLIC_BASE) with sftp.file("/tmp/_align_cpu_api.conf", "w") as f: f.write(CPU_API) with sftp.file("/tmp/_align_cpu_web.conf", "w") as f: f.write(CPU_WEB) with sftp.file("/tmp/_align_ffmpeg.slice", "w") as f: f.write(FFMPEG_SLICE) with sftp.file("/tmp/_align_api_watch.service", "w") as f: f.write(API_WATCH) with sftp.file("/tmp/_align_remote.sh", "w") as f: f.write(REMOTE) sftp.close() # Also ship same app code to dylive (parity; leave its MemoryHigh alone). print("=== dylive code parity ===", flush=True) _, o, e = c.exec_command( "cp -a /tmp/_align_host_metrics.py /opt/douyinWEB/backend/app/host_metrics.py; " "cp -a /tmp/_align_main.py /opt/douyinWEB/backend/app/main.py; " "cp -a /tmp/_align_yt_long_pipeline.py /opt/douyinWEB/backend/app/yt_long_pipeline.py; " "cp -a /tmp/_align_shorts_pipeline.py /opt/douyinWEB/backend/app/shorts_pipeline.py; " "cp -a /tmp/_align_tiktok_pipeline.py /opt/douyinWEB/backend/app/tiktok_pipeline.py; " "cp -a /tmp/_align_x_clips_pipeline.py /opt/douyinWEB/backend/app/x_clips_pipeline.py; " "cd /opt/douyinWEB/backend && .venv/bin/python -m py_compile " "app/host_metrics.py app/main.py app/yt_long_pipeline.py app/shorts_pipeline.py " "app/tiktok_pipeline.py app/x_clips_pipeline.py && " "systemctl restart douyinweb-api && " "for i in $(seq 1 15); do c=$(curl -sS -m 3 -o /dev/null -w %{http_code} " "http://127.0.0.1:8014/api/health || echo x); echo dylive:$c; [ \"$c\" = 200 ] && break; sleep 2; done", timeout=90, ) print((o.read() + e.read()).decode("utf-8", "replace")[-3000:], flush=True) print("=== dy 108 deploy ===", flush=True) chan = c.get_transport().open_session() chan.exec_command( f"SSHPASS={PASS} sshpass -e scp -o StrictHostKeyChecking=no " "/tmp/_align_host_metrics.py /tmp/_align_main.py /tmp/_align_yt_long_pipeline.py " "/tmp/_align_shorts_pipeline.py /tmp/_align_tiktok_pipeline.py /tmp/_align_x_clips_pipeline.py " "/tmp/_align_procutil.py /tmp/_align_youtube_relay.py /tmp/_align_yt_long_clip.py " "/tmp/_align_shorts_clip.py /tmp/_align_clip_cascade.py /tmp/_align_api_health_watch.sh " "/tmp/_align_oom.conf /tmp/_align_override.conf /tmp/_align_public.conf " "/tmp/_align_cpu_api.conf /tmp/_align_cpu_web.conf /tmp/_align_ffmpeg.slice " "/tmp/_align_api_watch.service /tmp/_align_remote.sh " f"root@{DY}:/tmp/ && " f"SSHPASS={PASS} sshpass -e ssh -o StrictHostKeyChecking=no root@{DY} " "'bash /tmp/_align_remote.sh' > /tmp/_align_dy.out 2>&1; echo EXIT:$? >> /tmp/_align_dy.out" ) out = "" for i in range(90): time.sleep(2) _, o2, _ = c.exec_command("tail -c 25000 /tmp/_align_dy.out 2>/dev/null", timeout=20) out = o2.read().decode("utf-8", "replace") if "EXIT:" in out: print(out[-12000:], flush=True) break if i % 5 == 0: print("poll", i, out[-160:].replace("\n", " | "), flush=True) else: print("TIMEOUT", out[-4000:], flush=True) c.close() return 1 print("=== public ===", flush=True) for url in ( "https://dy.nomadro.live/api/health", "https://dylive.nomadro.com/api/health", "https://dy.nomadro.live/", ): try: with httpx.Client(timeout=20, verify=False, follow_redirects=False) as h: r = h.get(url) print(url, r.status_code, f"t~{r.elapsed.total_seconds():.3f}", (r.text or "")[:100].replace("\n", " "), flush=True) except Exception as ex: print(url, "ERR", ex, flush=True) c.close() return 0 if "DY_ALIGN_DONE" in out and "EXIT:0" in out else 1 if __name__ == "__main__": raise SystemExit(main())