240 lines
7.5 KiB
Bash
240 lines
7.5 KiB
Bash
#!/usr/bin/env bash
|
|
# Soft-heal assign/ops Neko when local HTTP dies. Never wipes profiles/volumes.
|
|
# Only: docker restart <container> after consecutive failures + cooldown.
|
|
set +e
|
|
|
|
INTERVAL="${DY_NEKO_WATCH_INTERVAL:-30}"
|
|
FAILS_NEED="${DY_NEKO_WATCH_FAILS:-3}"
|
|
COOLDOWN="${DY_NEKO_WATCH_COOLDOWN:-300}"
|
|
STATE_DIR="${DY_NEKO_WATCH_STATE_DIR:-/run/douyinweb-neko-watch}"
|
|
ASSIGN_MAX="${DY_NEKO_WATCH_ASSIGN_MAX:-8}"
|
|
OPS_MAX="${DY_NEKO_WATCH_OPS_MAX:-8}"
|
|
|
|
log() { printf '[neko-watch] %s\n' "$*"; }
|
|
|
|
mkdir -p "$STATE_DIR" 2>/dev/null || true
|
|
|
|
http_ok() {
|
|
local port="$1" code
|
|
code=$(curl -sS -m 3 -o /dev/null -w '%{http_code}' "http://127.0.0.1:${port}/" 2>/dev/null)
|
|
[ -n "$code" ] || code=000
|
|
case "$code" in
|
|
200|401|403) return 0 ;;
|
|
*) return 1 ;;
|
|
esac
|
|
}
|
|
|
|
fail_file() { echo "${STATE_DIR}/${1}.fails"; }
|
|
cool_file() { echo "${STATE_DIR}/${1}.last_restart"; }
|
|
|
|
bump_fail() {
|
|
local key="$1" f n
|
|
f="$(fail_file "$key")"
|
|
n="$(cat "$f" 2>/dev/null || echo 0)"
|
|
case "$n" in ''|*[!0-9]*) n=0 ;; esac
|
|
n=$((n + 1))
|
|
echo "$n" >"$f"
|
|
echo "$n"
|
|
}
|
|
|
|
reset_fail() {
|
|
echo 0 >"$(fail_file "$1")"
|
|
}
|
|
|
|
maybe_restart() {
|
|
local key="$1" name="$2" fails now last elapsed
|
|
fails="$(cat "$(fail_file "$key")" 2>/dev/null || echo 0)"
|
|
case "$fails" in ''|*[!0-9]*) fails=0 ;; esac
|
|
if [ "$fails" -lt "$FAILS_NEED" ]; then
|
|
return 0
|
|
fi
|
|
now=$(date +%s)
|
|
last="$(cat "$(cool_file "$key")" 2>/dev/null || echo 0)"
|
|
case "$last" in ''|*[!0-9]*) last=0 ;; esac
|
|
elapsed=$((now - last))
|
|
if [ "$elapsed" -lt "$COOLDOWN" ]; then
|
|
log "$name fails=$fails cooldown ${elapsed}/${COOLDOWN}s"
|
|
return 0
|
|
fi
|
|
if ! docker inspect "$name" >/dev/null 2>&1; then
|
|
log "$name missing skip"
|
|
reset_fail "$key"
|
|
return 0
|
|
fi
|
|
running=$(docker inspect -f '{{.State.Running}}' "$name" 2>/dev/null || echo false)
|
|
if [ "$running" != "true" ]; then
|
|
log "starting stopped $name"
|
|
docker start "$name" >/dev/null 2>&1
|
|
else
|
|
log "docker restart $name (http dead, fails=$fails)"
|
|
docker restart "$name" >/dev/null 2>&1
|
|
fi
|
|
echo "$now" >"$(cool_file "$key")"
|
|
reset_fail "$key"
|
|
}
|
|
|
|
check_one() {
|
|
local key="$1" name="$2" port="$3"
|
|
if ! docker inspect "$name" >/dev/null 2>&1; then
|
|
return 0
|
|
fi
|
|
# Soft-start containers killed by OOM (Running=false, OOMKilled=true). Never wipe volumes.
|
|
oom=$(docker inspect -f '{{.State.OOMKilled}}' "$name" 2>/dev/null || echo false)
|
|
running=$(docker inspect -f '{{.State.Running}}' "$name" 2>/dev/null || echo false)
|
|
if [ "$running" != "true" ] && [ "$oom" = "true" ]; then
|
|
n=$(bump_fail "$key")
|
|
log "$name OOMKilled fails=$n/$FAILS_NEED"
|
|
maybe_restart "$key" "$name"
|
|
return 0
|
|
fi
|
|
# Only watch containers that exist; if intentionally stopped, skip soft-heal
|
|
# unless it was supposed to be running — we only heal Running or Restarting.
|
|
state=$(docker inspect -f '{{.State.Status}}' "$name" 2>/dev/null || echo "")
|
|
case "$state" in
|
|
running|restarting) ;;
|
|
*) return 0 ;;
|
|
esac
|
|
if ! http_ok "$port"; then
|
|
n=$(bump_fail "$key")
|
|
log "$name :$port down fails=$n/$FAILS_NEED"
|
|
maybe_restart "$key" "$name"
|
|
return 0
|
|
fi
|
|
# HTTP alive but Chrome DevTools dead → still restart (common after SIGILL/OOM-in-chrome).
|
|
if ! cdp_ok "$name"; then
|
|
n=$(bump_fail "$key")
|
|
log "$name CDP:9222 dead (HTTP ok) fails=$n/$FAILS_NEED"
|
|
maybe_restart "$key" "$name"
|
|
return 0
|
|
fi
|
|
# CDP up but renderer Aw Snap / SIGILL (tab title often still "Creator Studio").
|
|
if chrome_crash_ui "$name"; then
|
|
n=$(bump_fail "$key")
|
|
log "$name Chrome crash UI / SIGILL fails=$n/$FAILS_NEED"
|
|
# Prefer soft Chrome restart inside container; fall back to docker restart via maybe_restart.
|
|
if soft_restart_chrome "$name"; then
|
|
echo "$(date +%s)" >"$(cool_file "$key")"
|
|
reset_fail "$key"
|
|
return 0
|
|
fi
|
|
maybe_restart "$key" "$name"
|
|
return 0
|
|
fi
|
|
reset_fail "$key"
|
|
}
|
|
|
|
soft_restart_chrome() {
|
|
local name="$1"
|
|
docker exec "$name" bash -lc \
|
|
"supervisorctl restart google-chrome 2>/dev/null || supervisorctl restart chromium 2>/dev/null || pkill -TERM -f '/opt/google/chrome/chrome|google-chrome|chromium' 2>/dev/null; echo ok" \
|
|
>/dev/null 2>&1
|
|
}
|
|
|
|
chrome_crash_ui() {
|
|
local name="$1" out
|
|
# Recent SIGILL in chrome log (mtime within 10m).
|
|
out=$(docker exec "$name" bash -lc '
|
|
p=/var/log/neko/google-chrome.log
|
|
[ -f "$p" ] || exit 1
|
|
# file touched in last 600s?
|
|
find "$p" -mmin -10 2>/dev/null | grep -q . || exit 1
|
|
grep -aEi "SIGILL|Illegal instruction|Aw, Snap|STATUS_INVALID" "$p" >/dev/null 2>&1
|
|
' 2>/dev/null) && return 0
|
|
# Live Studio tab + evaluate timeout / crash text (fast 5s probe).
|
|
out=$(docker exec "$name" python3 - <<'PY' 2>/dev/null
|
|
import base64,json,os,socket,struct,sys,time,urllib.request
|
|
try:
|
|
tabs=json.load(urllib.request.urlopen("http://127.0.0.1:9222/json/list",timeout=2))
|
|
except Exception:
|
|
sys.exit(1)
|
|
pages=[t for t in tabs if t.get("type")=="page"]
|
|
pick=None
|
|
for t in pages:
|
|
u=(t.get("url") or "").lower()
|
|
if "studio.youtube.com" in u and "livestreaming" in u:
|
|
pick=t; break
|
|
if not pick:
|
|
sys.exit(1)
|
|
ws=pick.get("webSocketDebuggerUrl") or ""
|
|
if not ws.startswith("ws://"):
|
|
sys.exit(1)
|
|
hostport,path=ws[5:].split("/",1); path="/"+path
|
|
host,port=hostport.split(":"); port=int(port)
|
|
key=base64.b64encode(os.urandom(16)).decode()
|
|
try:
|
|
s=socket.create_connection((host,port),timeout=3); s.settimeout(3)
|
|
s.sendall(f"GET {path} HTTP/1.1\r\nHost: {hostport}\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Key: {key}\r\nSec-WebSocket-Version: 13\r\n\r\n".encode())
|
|
buf=b""
|
|
while b"\r\n\r\n" not in buf:
|
|
c=s.recv(2048)
|
|
if not c: sys.exit(2)
|
|
buf+=c
|
|
expr="(document.title||'')+' '+((document.body&&document.body.innerText)||'')[:500]"
|
|
data=json.dumps({"id":1,"method":"Runtime.evaluate","params":{"expression":expr,"returnByValue":True}}).encode()
|
|
mask=os.urandom(4); h=bytearray([0x81,0x80|len(data)]); h.extend(mask)
|
|
s.sendall(bytes(h)+bytes(b^mask[i%4] for i,b in enumerate(data)))
|
|
def ex(n):
|
|
o=b""
|
|
while len(o)<n:
|
|
c=s.recv(n-len(o))
|
|
if not c: raise RuntimeError("x")
|
|
o+=c
|
|
return o
|
|
deadline=time.time()+3
|
|
text=""
|
|
while time.time()<deadline:
|
|
b1,b2=ex(2); ln=b2&0x7f
|
|
if ln==126: ln=struct.unpack("!H",ex(2))[0]
|
|
elif ln==127: ln=struct.unpack("!Q",ex(8))[0]
|
|
payload=ex(ln)
|
|
try:
|
|
msg=json.loads(payload.decode())
|
|
except Exception:
|
|
continue
|
|
if msg.get("id")==1:
|
|
text=str((((msg.get("result") or {}).get("result") or {}).get("value")) or "")
|
|
break
|
|
s.close()
|
|
except Exception:
|
|
# Evaluate hang/timeout while Live tab open => wedged renderer (Aw Snap).
|
|
sys.exit(0)
|
|
if not text:
|
|
sys.exit(0)
|
|
low=text.lower()
|
|
if "sigill" in low or "aw, snap" in low or "aw snap" in low:
|
|
sys.exit(0)
|
|
sys.exit(1)
|
|
PY
|
|
)
|
|
[ $? -eq 0 ]
|
|
}
|
|
|
|
cdp_ok() {
|
|
local name="$1"
|
|
# DevTools listens inside the container only.
|
|
docker exec "$name" curl -fsS -m 2 http://127.0.0.1:9222/json/version >/dev/null 2>&1 && return 0
|
|
docker exec "$name" wget -q -T 2 -O /dev/null http://127.0.0.1:9222/json/version >/dev/null 2>&1 && return 0
|
|
return 1
|
|
}
|
|
|
|
log "interval=${INTERVAL}s fails=${FAILS_NEED} cooldown=${COOLDOWN}s assign=1..${ASSIGN_MAX} ops=1..${OPS_MAX}"
|
|
|
|
while true; do
|
|
if ! command -v docker >/dev/null 2>&1; then
|
|
log "no docker"
|
|
sleep "$INTERVAL"
|
|
continue
|
|
fi
|
|
i=1
|
|
while [ "$i" -le "$ASSIGN_MAX" ]; do
|
|
check_one "assign-$i" "douyinweb-neko-$i" $((9200 + i - 1))
|
|
i=$((i + 1))
|
|
done
|
|
i=1
|
|
while [ "$i" -le "$OPS_MAX" ]; do
|
|
check_one "ops-$i" "douyinweb-ops-neko-$i" $((18200 + i - 1))
|
|
i=$((i + 1))
|
|
done
|
|
sleep "$INTERVAL"
|
|
done
|