Stop Next build usr/node orphans from pinning the 4-core VPS; Cap web CPU and defer clips earlier under load. Co-authored-by: Cursor <cursoragent@cursor.com>
173 lines
5.5 KiB
Python
173 lines
5.5 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Permanent dy VPS stabilize: orphan watch + web cgroup kill + CPU caps."""
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
|
|
import paramiko
|
|
|
|
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
PASS = "Xiao4669805"
|
|
JUMP = "107.173.30.245"
|
|
DY = "108.174.50.68"
|
|
|
|
WATCH_SH = (ROOT / "scripts/linux/build_orphan_watch.sh").read_text(encoding="utf-8")
|
|
|
|
WATCH_SERVICE = """[Unit]
|
|
Description=douyinWEB kill Next/npm build CPU orphans
|
|
After=network-online.target
|
|
|
|
[Service]
|
|
Type=oneshot
|
|
ExecStart=/bin/bash /opt/douyinWEB/scripts/linux/build_orphan_watch.sh
|
|
Nice=10
|
|
"""
|
|
|
|
WATCH_TIMER = """[Unit]
|
|
Description=Run build orphan watch every minute
|
|
|
|
[Timer]
|
|
OnBootSec=1min
|
|
OnUnitActiveSec=1min
|
|
AccuracySec=15s
|
|
Unit=douyinweb-build-orphan-watch.service
|
|
|
|
[Install]
|
|
WantedBy=timers.target
|
|
"""
|
|
|
|
WEB_HARDEN = """[Service]
|
|
# Orphans from npm/next must die with the unit (PPID 1 still in this cgroup).
|
|
KillMode=control-group
|
|
KillSignal=SIGTERM
|
|
TimeoutStopSec=20
|
|
# Cap web so a runaway next/node cannot pin the whole 4-core box.
|
|
CPUAccounting=yes
|
|
CPUQuota=150%
|
|
CPUWeight=40
|
|
Nice=5
|
|
MemoryAccounting=yes
|
|
MemoryHigh=900M
|
|
MemoryMax=1200M
|
|
"""
|
|
|
|
# Also tighten API background when possible — keep existing MemoryHigh from align.
|
|
API_CPU = """[Service]
|
|
Nice=0
|
|
CPUWeight=120
|
|
# Leave headroom for live ffmpeg + Neko; API itself should not take all cores.
|
|
CPUQuota=200%
|
|
"""
|
|
|
|
REMOTE = r'''
|
|
set -e
|
|
install -d -m 755 /opt/douyinWEB/scripts/linux
|
|
install -d -m 755 /etc/systemd/system/douyinweb-web.service.d
|
|
install -d -m 755 /etc/systemd/system/douyinweb-api.service.d
|
|
|
|
install -m 755 /tmp/_stab_orphan.sh /opt/douyinWEB/scripts/linux/build_orphan_watch.sh
|
|
install -m 644 /tmp/_stab_orphan.service /etc/systemd/system/douyinweb-build-orphan-watch.service
|
|
install -m 644 /tmp/_stab_orphan.timer /etc/systemd/system/douyinweb-build-orphan-watch.timer
|
|
install -m 644 /tmp/_stab_web.conf /etc/systemd/system/douyinweb-web.service.d/harden.conf
|
|
install -m 644 /tmp/_stab_api_cpu.conf /etc/systemd/system/douyinweb-api.service.d/cpu.conf
|
|
# tighter clip defer under load
|
|
if [ -f /tmp/_stab_host_metrics.py ]; then
|
|
cp -a /tmp/_stab_host_metrics.py /opt/douyinWEB/backend/app/host_metrics.py
|
|
fi
|
|
|
|
# Immediate orphan sweep
|
|
bash /opt/douyinWEB/scripts/linux/build_orphan_watch.sh || true
|
|
pkill -9 -x usr 2>/dev/null || true
|
|
|
|
systemctl daemon-reload
|
|
systemctl enable --now douyinweb-build-orphan-watch.timer
|
|
systemctl restart douyinweb-web
|
|
systemctl try-restart douyinweb-api || systemctl restart douyinweb-api
|
|
sleep 4
|
|
|
|
# Ensure ffmpeg slice CPUQuota still 200%
|
|
systemctl cat douyinweb-ffmpeg.slice | grep -E 'CPUQuota|Memory' || true
|
|
systemctl is-active douyinweb-api douyinweb-web douyinweb-api-watch douyinweb-build-orphan-watch.timer
|
|
systemctl list-timers 'douyinweb-build-orphan-watch*' --no-pager | head -5
|
|
|
|
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 health:$c
|
|
[ "$c" = "200" ] && break
|
|
sleep 2
|
|
done
|
|
uptime
|
|
ps -eo pid,pcpu,etime,cmd --sort=-pcpu | head -10
|
|
echo STAB_DONE
|
|
'''
|
|
|
|
|
|
def wait_out(c: paramiko.SSHClient, path: str, token: str = "STAB_DONE", rounds: int = 60) -> str:
|
|
out = ""
|
|
for i in range(rounds):
|
|
time.sleep(2)
|
|
_, o, _ = c.exec_command(f"tail -c 20000 {path} 2>/dev/null", timeout=15)
|
|
out = o.read().decode("utf-8", "replace")
|
|
if token in out and "EXIT:" in out:
|
|
return out
|
|
if i % 5 == 0:
|
|
print("poll", i, out[-100:].replace("\n", " | "), flush=True)
|
|
return out
|
|
|
|
|
|
def stage(sftp: paramiko.SFTPClient) -> None:
|
|
with sftp.file("/tmp/_stab_orphan.sh", "w") as f:
|
|
f.write(WATCH_SH.replace("\r\n", "\n"))
|
|
with sftp.file("/tmp/_stab_orphan.service", "w") as f:
|
|
f.write(WATCH_SERVICE)
|
|
with sftp.file("/tmp/_stab_orphan.timer", "w") as f:
|
|
f.write(WATCH_TIMER)
|
|
with sftp.file("/tmp/_stab_web.conf", "w") as f:
|
|
f.write(WEB_HARDEN)
|
|
with sftp.file("/tmp/_stab_api_cpu.conf", "w") as f:
|
|
f.write(API_CPU)
|
|
hm = (ROOT / "backend/app/host_metrics.py").read_bytes().replace(b"\r\n", b"\n")
|
|
with sftp.file("/tmp/_stab_host_metrics.py", "wb") as f:
|
|
f.write(hm)
|
|
with sftp.file("/tmp/_stab_remote.sh", "w") as f:
|
|
f.write(REMOTE)
|
|
|
|
|
|
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()
|
|
stage(sftp)
|
|
sftp.close()
|
|
|
|
print("===== dylive =====", flush=True)
|
|
c.exec_command(
|
|
"bash /tmp/_stab_remote.sh > /tmp/_stab_dylive.out 2>&1; echo EXIT:$? >> /tmp/_stab_dylive.out",
|
|
timeout=1,
|
|
)
|
|
print(wait_out(c, "/tmp/_stab_dylive.out")[-9000:], flush=True)
|
|
|
|
print("===== dy (priority) =====", flush=True)
|
|
files = (
|
|
"/tmp/_stab_orphan.sh /tmp/_stab_orphan.service /tmp/_stab_orphan.timer "
|
|
"/tmp/_stab_web.conf /tmp/_stab_api_cpu.conf /tmp/_stab_host_metrics.py /tmp/_stab_remote.sh"
|
|
)
|
|
c.exec_command(
|
|
f"SSHPASS={PASS} sshpass -e scp -o StrictHostKeyChecking=no {files} root@{DY}:/tmp/ && "
|
|
f"SSHPASS={PASS} sshpass -e ssh -o StrictHostKeyChecking=no root@{DY} "
|
|
f"'bash /tmp/_stab_remote.sh' > /tmp/_stab_dy.out 2>&1; echo EXIT:$? >> /tmp/_stab_dy.out",
|
|
timeout=1,
|
|
)
|
|
print(wait_out(c, "/tmp/_stab_dy.out")[-9000:], flush=True)
|
|
c.close()
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|