Files
ProxyPool/script/start.bat
祀梦 df3cc87f88 fix(scripts): 修复 Windows 启动脚本兼容性和稳定性问题
- start.bat: 移除后端日志文件重定向,避免文件锁定导致启动失败

- start.bat: 用 ping 替代 timeout,修复 PowerShell 下错误

- start.bat: 健康检查改为 health 接口,提升可靠性

- start.bat: 修复前端 cd 路径带空格时的引号嵌套问题,使用 /B 在当前窗口后台运行

- stop.bat: 同样用 ping 替代 timeout 提升兼容性
2026-04-02 22:19:59 +08:00

89 lines
2.2 KiB
Batchfile

@echo off
chcp 65001 >nul
echo === ProxyPool Startup ===
echo.
set "ROOT_PATH=%~dp0.."
set "BACKEND_PORT=9949"
set "FRONTEND_PORT=9948"
REM 1. Clean processes on ports
echo [1/4] Cleaning old processes...
for /f "tokens=5" %%a in ('netstat -ano ^| findstr ":%BACKEND_PORT%" ^| findstr "LISTENING"') do (
taskkill /F /PID %%a >nul 2>&1
echo Stopped backend (PID: %%a)
)
for /f "tokens=5" %%a in ('netstat -ano ^| findstr ":%FRONTEND_PORT%" ^| findstr "LISTENING"') do (
taskkill /F /PID %%a >nul 2>&1
echo Stopped frontend (PID: %%a)
)
echo Cleanup complete!
echo.
REM 2. Start Backend
echo [2/4] Starting backend (FastAPI)...
if exist "%ROOT_PATH%\venv\Scripts\python.exe" (
set "PYTHON_PATH=%ROOT_PATH%\venv\Scripts\python.exe"
echo Using venv
) else (
set "PYTHON_PATH=python"
echo Using system Python
)
cd /d "%ROOT_PATH%"
set "PYTHONIOENCODING=utf-8"
REM Ensure logs directory exists (used by application logger)
mkdir "%ROOT_PATH%\logs" 2>nul
REM Start backend in background of current console
start /B "" "%PYTHON_PATH%" -u main.py
echo Backend started
echo.
REM 3. Wait for backend
echo [3/4] Waiting for backend...
set RETRY_COUNT=0
set BACKEND_READY=0
:WAIT_LOOP
if %RETRY_COUNT% geq 15 goto WAIT_DONE
REM Use ping instead of timeout for PowerShell compatibility
ping -n 3 127.0.0.1 >nul 2>&1
set /a RETRY_COUNT+=1
REM Try to connect to backend health endpoint
powershell -Command "try { $r = Invoke-RestMethod -Uri 'http://127.0.0.1:9949/health' -TimeoutSec 2 -ErrorAction Stop; exit 0 } catch { exit 1 }" >nul 2>&1
if %errorlevel% equ 0 (
set BACKEND_READY=1
goto WAIT_DONE
)
echo Waiting... (%RETRY_COUNT%/15)
goto WAIT_LOOP
:WAIT_DONE
if %BACKEND_READY% equ 0 (
echo.
echo Backend failed to start within 30 seconds!
echo Check the console output above for errors.
pause
exit /b 1
)
echo Backend is ready!
echo.
REM 4. Start Frontend
echo [4/4] Starting frontend (Vite)...
cd /d "%ROOT_PATH%\frontend"
start /B "" cmd /c "npm run dev"
cd /d "%ROOT_PATH%"
echo Frontend started
echo.
echo === All services started ===
echo Backend: http://127.0.0.1:9949
echo Frontend: http://localhost:9948
echo.
echo Press any key to close this window (services will keep running).
pause >nul