ain8n / run.sh
orztv
update
2d97948
raw
history blame
2.86 kB
#!/bin/bash
set -eo pipefail
# 导入环境变量
source /home/pn/.env
# 错误处理函数
handle_error() {
echo "错误发生在第 $1 行"
exit 1
}
trap 'handle_error $LINENO' ERR
# 超时处理函数
timeout_handler() {
echo "操作超时"
exit 1
}
# 等待服务就绪的通用函数
wait_for_service() {
local service=$1
local host=$2
local port=$3
local timeout=${4:-$WAIT_TIMEOUT}
echo "等待 $service 就绪..."
local end=$((SECONDS + timeout))
while [ $SECONDS -lt $end ]; do
if nc -z "$host" "$port" >/dev/null 2>&1; then
echo "$service 已就绪"
return 0
fi
echo "尝试连接 $service at $host:$port..."
sleep 1
done
echo "$service 启动超时"
exit 1
}
# 启动 Redis 服务
start_redis() {
echo "Starting Redis server..."
redis-server --daemonize yes
sleep 1
if ! redis-cli ping > /dev/null 2>&1; then
echo "Failed to start Redis server"
exit 1
fi
# 设置 Redis 配置
redis-cli config set maxmemory 512mb
redis-cli config set maxmemory-policy allkeys-lru
echo "Redis server started successfully"
}
# 启动 Qdrant 服务
start_qdrant() {
echo "Starting Qdrant server..."
mkdir -p /home/pn/.n8n/qdrant
qdrant > /home/pn/.n8n/qdrant/qdrant.log 2>&1 &
# 等待 Qdrant 启动
local timeout=30
local end=$((SECONDS + timeout))
while [ $SECONDS -lt $end ]; do
if curl -s http://localhost:6333/health >/dev/null; then
echo "Qdrant server started successfully"
return 0
fi
sleep 1
done
echo "Failed to start Qdrant server"
exit 1
}
# 检查服务状态
check_services() {
echo "检查服务状态..."
# 检查 Redis
echo "Redis 状态:"
redis-cli info | grep 'used_memory\|connected_clients\|total_connections_received'
# 检查 Redis 队列
echo "Redis 队列状态:"
redis-cli keys "${N8N_QUEUE_BULL_REDIS_PREFIX}*"
# 检查 Qdrant
echo "Qdrant 状态:"
curl -s http://localhost:6333/metrics
}
# 主流程
main() {
current_time=$(date +"%Y-%m-%d %H:%M:%S")
echo "Starting services at $current_time"
# 输出配置信息
echo "Database Configuration:"
echo "Host: ${DB_POSTGRESDB_HOST}"
echo "Port: ${DB_POSTGRESDB_PORT}"
echo "User: ${DB_POSTGRESDB_USER}"
echo "Database: ${DB_POSTGRESDB_DATABASE}"
echo "Type: ${DB_TYPE}"
# 启动服务
wait_for_service "PostgreSQL" "${DB_POSTGRESDB_HOST}" "${DB_POSTGRESDB_PORT}"
start_redis
start_qdrant
check_services
# 设置 N8N 环境变量
source /home/pn/n8n/config/n8n_env.sh
echo "Starting n8n..."
exec n8n start
}
# 执行主流程
main "$@"