chore: 优化 AI-Core 启动脚本

- 更新 start.bat 和 start.sh 启动脚本
- 优化 gRPC 服务器配置

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-09 12:50:33 +08:00
parent dc1c825d2e
commit 16b6aa0004
3 changed files with 77 additions and 30 deletions

View File

@@ -222,7 +222,7 @@ def serve(port: int = 50051, max_workers: int = 10):
server=server,
)
server.add_insecure_port(f"[::]:{port}")
server.add_insecure_port(f"0.0.0.0:{port}")
server.start()
logger.info("DocumentParser gRPC server (MarkItDown) started on port %d", port)

View File

@@ -1,13 +1,36 @@
@echo off
chcp 65001 >nul
echo Starting AI-Core Document Parser gRPC Server...
set PORT=50051
echo Checking and cleaning up port %PORT%...
for /f "tokens=5" %%a in ('netstat -ano ^| findstr :%PORT% ^| findstr LISTENING') do (
echo Killing process %%a on port %PORT%...
taskkill /F /PID %%a 2>nul
)
timeout /t 2 /nobreak >nul
cd /d %~dp0
echo Starting AI-Core Service...
echo.
REM 激活虚拟环境
call venv\Scripts\activate.bat
echo Using virtual environment Python...
if exist "venv\Scripts\python.exe" (
set PYTHON_CMD=%~dp0venv\Scripts\python.exe
) else (
set PYTHON_CMD=py
)
REM 启动服务
python main.py %*
echo Using Python: %PYTHON_CMD%
%PYTHON_CMD% --version
REM 如果按任意键退出
pause
echo Checking port %PORT%...
%PYTHON_CMD% -c "import socket; s=socket.socket(); s.settimeout(1); r=s.connect_ex(('127.0.0.1',%PORT%)); s.close(); exit(0 if r!=0 else 1)" 2>nul
if %ERRORLEVEL% NEQ 0 (
echo Port %PORT% is free, starting server...
) else (
echo Port %PORT% is still in use, please check manually
exit /b 1
)
echo Starting server on port %PORT%...
%PYTHON_CMD% main.py --port %PORT% --max-workers 10 --log-level INFO

View File

@@ -58,29 +58,53 @@ if [ ! -f "proto/document_parser_pb2.py" ]; then
fi
fi
# 检查端口占用并释放
echo "Checking port $PORT..."
if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "win32" ]]; then
# Windows
NETSTAT_OUTPUT=$(netstat -ano 2>/dev/null | grep ":$PORT" | grep LISTENING)
if [ -n "$NETSTAT_OUTPUT" ]; then
echo "Port $PORT is in use, killing process..."
PID=$(echo "$NETSTAT_OUTPUT" | awk '{print $NF}' | head -1)
if [ -n "$PID" ]; then
taskkill //F //PID $PID 2>/dev/null
sleep 1
fi
fi
else
# Linux/Mac
PID=$(lsof -ti:$PORT 2>/dev/null)
if [ -n "$PID" ]; then
echo "Port $PORT is in use, killing process $PID..."
kill $PID 2>/dev/null
sleep 1
fi
# 用 Python 来检测和杀死占用端口的进程(跨平台更可靠)
echo "Checking and cleaning up port $PORT..."
# 先尝试直接用 Windows 命令杀死(更可靠)
if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "win32" || "$(uname)" == "MINGW"* ]]; then
# 直接用 cmd /c 执行
cmd //c "for /f \"tokens=5\" %a in ('netstat -ano ^| findstr :$PORT ^| findstr LISTENING') do taskkill /F /PID %a"
sleep 1
fi
# 再用 Python 检测
$PYTHON_CMD -c "
import socket
import subprocess
import sys
import time
import os
port = $PORT
print(f'Checking port {port}...')
# 检查端口是否被占用
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(1)
result = s.connect_ex(('127.0.0.1', port))
s.close()
if result != 0:
print(f'Port {port} is free (not listening)')
else:
print(f'Port {port} is still in use!')
# 尝试杀死
try:
result = subprocess.run(['netstat', '-ano'], capture_output=True, text=True, shell=True)
for line in result.stdout.split('\n'):
if f':{port}' in line and 'LISTENING' in line:
parts = line.split()
pid = parts[-1]
print(f'Found process {pid}, killing...')
os.system(f'taskkill /F /PID {pid}')
time.sleep(2)
except Exception as e:
print(f'Error: {e}')
except Exception as e:
print(f'Check error: {e}')
"
# Start the server
echo "Starting server on port $PORT..."
$PYTHON_CMD main.py --port $PORT --max-workers 10 --log-level INFO