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

63 lines
1.7 KiB
Python

#!/usr/bin/env python3
"""Tighten dylive API memory drop-in in-place (wins over other drop-ins)."""
from __future__ import annotations
import sys
import time
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(
"107.173.30.245",
22,
"root",
"Xiao4669805",
timeout=45,
allow_agent=False,
look_for_keys=False,
banner_timeout=120,
)
cmd = r"""
set -euo pipefail
# Single source of truth — later-sorted name so it wins
rm -f /etc/systemd/system/douyinweb-api.service.d/memory.conf
cat > /etc/systemd/system/douyinweb-api.service.d/oom-protect-chrome.conf <<'EOF'
[Service]
MemoryAccounting=yes
# Soft reclaim under pressure; hard kill before host global OOM (hist ~2G RSS)
MemoryHigh=700M
MemoryMax=900M
MemorySwapMax=256M
OOMPolicy=kill
# Prefer kill API over Neko Chrome (chrome oom_score_adj=-800)
OOMScoreAdjust=400
EOF
systemctl daemon-reload
systemctl restart douyinweb-api.service
sleep 6
systemctl is-active douyinweb-api.service
echo '--- show ---'
systemctl show douyinweb-api.service -p MemoryCurrent,MemoryHigh,MemoryMax,MemorySwapMax,OOMPolicy,OOMScoreAdjust
CG=$(systemctl show douyinweb-api.service -p ControlGroup --value)
echo CG=$CG
for f in memory.max memory.high memory.current memory.swap.max memory.events; do
p="/sys/fs/cgroup${CG}/$f"
[ -f "$p" ] && echo "$f=$(cat $p)"
done
echo '--- health ---'
curl -sS -m 10 http://127.0.0.1:8014/api/health
echo
sleep 8
curl -sS -m 10 http://127.0.0.1:8014/api/health
echo
free -h | head -3
"""
_i, o, e = c.exec_command(cmd, timeout=120)
print((o.read() + e.read()).decode("utf-8", "replace"))
c.close()