#!/bin/bash # /opt/topfans/loadtest/recover/emergency-stop.sh # 一键灭火 set -e echo "🚨 emergency stop" pkill -9 loadgen 2>/dev/null || true PG_CONTAINER=$(docker ps --filter 'name=postgres' --format '{{.Names}}' | grep -v exporter | head -1) [ -z "$PG_CONTAINER" ] && { echo "❌ 找不到 postgres 容器"; exit 1; } export PGPASSWORD="${DB_PASSWORD:-postgres123}" docker exec -e PGPASSWORD="$PGPASSWORD" "$PG_CONTAINER" psql -U postgres -d topfans -c " SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE state != 'idle' AND now() - query_start > interval '10 seconds' AND usename = 'postgres'; " || true cd /opt/topfans/docker || { echo "❌ 找不到 /opt/topfans/docker"; exit 1; } docker-compose -f docker-compose.prod.yml restart sleep 30 if curl -fs http://localhost:8080/health; then echo "✅ gateway 恢复" else echo "⚠️ gateway 仍未恢复" exit 1 fi