#!/bin/bash
# 2026-10-09 重启 ComfyUI（保持与当前实例完全相同的参数）并确认 Veda 节点注册
set -u
PID=$(ss -ltnp 2>/dev/null | grep ":8189" | grep -oP 'pid=\K[0-9]+' | head -1)
echo "旧 PID=$PID"
[ -n "$PID" ] && kill "$PID"
sleep 5
echo "端口残留=$(ss -ltn | grep -c ':8189')"
cd /home/zyw/ComfyUI || exit 1
setsid nohup env PYTORCH_CUDA_ALLOC_CONF=max_split_size_mb:512,expandable_segments:True \
  CUDA_VISIBLE_DEVICES=0 OMP_NUM_THREADS=4 \
  venv/bin/python main.py --listen 0.0.0.0 --port 8189 \
  --lowvram --force-fp16 --reserve-vram 1.0 --fast-disk --cache-lru 2 --disable-pinned-memory \
  --preview-method none >> /home/zyw/comfyui_8189_opt.log 2>&1 < /dev/null &
echo "已启动，等待节点注册…"
for i in $(seq 1 40); do
  sleep 10
  CODE=$(curl -s -o /tmp/veda_node.json -w '%{http_code}' --noproxy '*' --max-time 10 \
         http://127.0.0.1:8189/object_info/VedaSparseAttention)
  if [ "$CODE" = "200" ]; then
    echo "✅ Veda 节点已注册（约 $((i*10))s）"
    python3 - <<'PY'
import json
d = json.load(open('/tmp/veda_node.json'))
k = list(d)[0]
info = d[k]
print("class_type:", k)
print("display:", info.get("display_name"))
print("required inputs:", list(info.get("input", {}).get("required", {})))
opts = info.get("input", {}).get("required", {}).get("predictor", [[], {}])[0]
print("predictor 选项:", opts[:3])
PY
    exit 0
  fi
  echo "  等待 ${i}0s (HTTP $CODE)"
done
echo "❌ 超时：节点未注册"
tail -40 /home/zyw/comfyui_8189_opt.log
exit 1
