#!/usr/bin/env bash
# 仅重启失败的子代理(保留 hub / 已 online 的 agent)
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
HUB_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)"
ECO="${HUB_DIR}/ecosystem.config.cjs"

cd "${HUB_DIR}"
chmod +x scripts/run_agent.sh scripts/run_hub.sh 2>/dev/null || true

AGENTS=(manual-agent-binance manual-agent-okx manual-agent-gate)

for n in "${AGENTS[@]}"; do
  pm2 delete "${n}" 2>/dev/null || true
done

pm2 start "${ECO}" --only manual-agent-binance
pm2 start "${ECO}" --only manual-agent-okx
pm2 start "${ECO}" --only manual-agent-gate

pm2 save 2>/dev/null || true
echo "已重建 binance / okx / gate 子代理,请执行: bash scripts/check_agents.sh"
