#!/bin/bash
# 强制重启所有服务

echo "=== 强制重启考培练系统服务 ==="
echo "时间: $(date)"

# 1. 停止所有相关进程
echo "1. 停止相关进程..."
pkill -f "docker-compose"
pkill -f "webhook_handler"

# 2. 清理Docker
echo "2. 清理Docker容器和网络..."
cd /root/aiedu
docker compose down --remove-orphans 2>/dev/null || true
docker system prune -f 2>/dev/null || true

# 3. 重启Docker服务
echo "3. 重启Docker服务..."
systemctl restart docker
sleep 15

# 4. 启动服务
echo "4. 启动考培练系统..."
cd /root/aiedu
docker compose up -d

# 5. 启动webhook服务
echo "5. 启动Webhook服务..."
systemctl restart kaopeilian-webhook.service

# 6. 等待服务启动
echo "6. 等待服务启动..."
sleep 30

# 7. 检查状态
echo "7. 检查服务状态..."
echo "Docker容器:"
docker ps --format "table {{.Names}}\t{{.Status}}" 2>/dev/null || echo "Docker命令失败"

echo ""
echo "端口监听:"
netstat -tlnp | grep -E ":(80|443|8000|3306|6379|9000)" || echo "端口检查失败"

echo ""
echo "网站测试:"
curl -I --connect-timeout 5 https://aiedu.ireborn.com.cn 2>/dev/null || echo "网站访问失败"

echo ""
echo "=== 重启完成 ==="
