流程图
代码解释
#!/bin/bash
set -e
load_env_file() {
local script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
local env_file="$script_dir/.env"
# 检查.env文件是否存在
if [ -f "$env_file" ]; then
echo "从以下位置加载环境变量: $env_file"
# 设置所有变量为环境变量
set -a
source "$env_file"
set +a
else
echo "警告: 未找到.env文件: $env_file"
fi
}
# 加载环境变量
load_env_file
# 取消可能由Docker守护进程设置的HTTP代理
export http_proxy=""; export https_proxy=""; export no_proxy=""; export HTTP_PROXY=""; export HTTPS_PROXY=""; export NO_PROXY=""
export PYTHONPATH=$(pwd)
export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu/
JEMALLOC_PATH=$(pkg-config --variable=libdir jemalloc)/libjemalloc.so
PY=python3
if [[ -z "$WS" || $WS -lt 1 ]]; then
WS=1
fi
MAX_RETRIES=5
STOP=false
PIDS=()
export NLTK_DATA="./nltk_data"
cleanup() {
echo "接收到终止信号。正在关闭..."
STOP=true
for pid in "${PIDS[@]}"; do
if kill -0 "$pid" 2>/dev/null; then
echo "正在终止进程 $pid"
kill "$pid"
fi
done
exit 0
}
trap cleanup SIGINT SIGTERM
task_exe(){
local task_id=$1
local retry_count=0
while ! $STOP && [ $retry_count -lt $MAX_RETRIES ]; do
echo "为任务 $task_id 启动task_executor.py (尝试 $((retry_count+1)))"
LD_PRELOAD=$JEMALLOC_PATH $PY rag/svr/task_executor.py "$task_id"
EXIT_CODE=$?
if [ $EXIT_CODE -eq 0 ]; then
echo "任务 $task_id 的task_executor.py成功退出。"
break
else
echo "任务 $task_id 的task_executor.py失败,退出代码 $EXIT_CODE。正在重试..." >&2
retry_count=$((retry_count + 1))
sleep 2
fi
done
if [ $retry_count -ge $MAX_RETRIES ]; then
echo "任务 $task_id 的task_executor.py在 $MAX_RETRIES 次尝试后失败。正在退出..." >&2
cleanup
fi
}
run_server(){
local retry_count=0
while ! $STOP && [ $retry_count -lt $MAX_RETRIES ]; do
echo "启动ragflow_server.py (尝试 $((retry_count+1)))"
$PY api/ragflow_server.py
EXIT_CODE=$?
if [ $EXIT_CODE -eq 0 ]; then
echo "ragflow_server.py成功退出。"
break
else
echo "ragflow_server.py失败,退出代码 $EXIT_CODE。正在重试..." >&2
retry_count=$((retry_count + 1))
sleep 2
fi
done
if [ $retry_count -ge $MAX_RETRIES ]; then
echo "ragflow_server.py在 $MAX_RETRIES 次尝试后失败。正在退出..." >&2
cleanup
fi
}
for ((i=0;i<WS;i++))
do
task_exe "$i" &
PIDS+=($!)
done
run_server &
PIDS+=($!)
wait