#!/bin/bash
cd "$(dirname "$0")"

# 停止现有进程
pkill -f "AliPack.jar" 2>/dev/null
rm -f AliPack.jar.pid

# 清理日志
mv app.log app.log.old 2>/dev/null

# 启动应用
echo "正在启动Java应用..."
nohup java -javaagent:AliPack.jar -jar -Xms1024m -Xmx2048m -Xmn192m -Xss512k -XX:+UseConcMarkSweepGC -XX:CMSFullGCsBeforeCompaction=200 -XX:+UseCMSCompactAtFullCollection AliPack.jar > app.log 2>&1 &
NEW_PID=$!
echo $NEW_PID > AliPack.jar.pid

echo "进程已启动，PID: $NEW_PID"
echo "等待应用启动（约20-30秒）..."

# 等待并检查
for i in {1..6}; do
    sleep 5
    if grep -q "Started SpringbootApplication" app.log 2>/dev/null; then
        echo "✓ 应用启动成功！"
        echo "访问地址: http://103.110.81.44:8033"
        exit 0
    fi
    echo -n "."
done

echo ""
echo "请查看日志确认: tail -f app.log"
