|
#!/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 |
|
} |
|
|
|
|
|
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-cli config set maxmemory 512mb |
|
redis-cli config set maxmemory-policy allkeys-lru |
|
echo "" |
|
echo "Redis server started successfully" |
|
echo "" |
|
} |
|
|
|
|
|
start_qdrant() { |
|
echo "Starting Qdrant server..." |
|
|
|
|
|
mkdir -p /home/pn/.n8n/qdrant/storage |
|
mkdir -p /home/pn/.n8n/qdrant/config |
|
mkdir -p /home/pn/.n8n/qdrant/snapshots |
|
mkdir -p /home/pn/.n8n/qdrant/logs |
|
|
|
|
|
chmod -R 755 /home/pn/.n8n/qdrant |
|
chown -R pn:pn /home/pn/.n8n/qdrant |
|
|
|
|
|
cat > /home/pn/.n8n/qdrant/config/config.yaml <<EOF |
|
service: |
|
host: 0.0.0.0 |
|
http_port: 6333 |
|
grpc_port: 6334 |
|
enable_cors: true |
|
enable_tls: false |
|
max_request_size_mb: 64 |
|
max_workers: 0 |
|
|
|
storage: |
|
storage_path: /home/pn/.n8n/qdrant/storage |
|
snapshots_path: /home/pn/.n8n/qdrant/snapshots |
|
on_disk_payload: true |
|
|
|
performance: |
|
max_search_threads: 0 |
|
max_optimization_threads: 0 |
|
|
|
optimizers: |
|
deleted_threshold: 0.2 |
|
vacuum_min_vector_number: 1000 |
|
default_segment_number: 0 |
|
max_segment_size_kb: null |
|
indexing_threshold_kb: 20000 |
|
flush_interval_sec: 5 |
|
|
|
hnsw_index: |
|
m: 16 |
|
ef_construct: 100 |
|
full_scan_threshold_kb: 10000 |
|
max_indexing_threads: 0 |
|
on_disk: false |
|
|
|
logger: |
|
on_disk: |
|
enabled: true |
|
log_file: /home/pn/.n8n/qdrant/logs/qdrant.log |
|
log_level: INFO |
|
|
|
telemetry_disabled: true |
|
EOF |
|
|
|
|
|
chmod 644 /home/pn/.n8n/qdrant/config/config.yaml |
|
|
|
|
|
qdrant --config-path /home/pn/.n8n/qdrant/config/config.yaml > /home/pn/.n8n/qdrant/logs/startup.log 2>&1 & |
|
|
|
|
|
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" |
|
|
|
echo "Qdrant version:" |
|
curl -s http://localhost:6333/version |
|
|
|
|
|
echo "Creating default collections..." |
|
|
|
|
|
curl -X PUT 'http://localhost:6333/collections/text_vectors' \ |
|
-H 'Content-Type: application/json' \ |
|
-d '{ |
|
"vectors": { |
|
"size": 768, |
|
"distance": "Cosine" |
|
}, |
|
"optimizers_config": { |
|
"default_segment_number": 2, |
|
"indexing_threshold": 20000 |
|
}, |
|
"hnsw_config": { |
|
"m": 16, |
|
"ef_construct": 100, |
|
"full_scan_threshold": 10000 |
|
} |
|
}' |
|
|
|
|
|
curl -X PUT 'http://localhost:6333/collections/image_vectors' \ |
|
-H 'Content-Type: application/json' \ |
|
-d '{ |
|
"vectors": { |
|
"size": 512, |
|
"distance": "Cosine" |
|
}, |
|
"optimizers_config": { |
|
"default_segment_number": 2, |
|
"indexing_threshold": 20000 |
|
}, |
|
"hnsw_config": { |
|
"m": 16, |
|
"ef_construct": 100, |
|
"full_scan_threshold": 10000 |
|
} |
|
}' |
|
|
|
|
|
curl -X PUT 'http://localhost:6333/collections/openai_vectors' \ |
|
-H 'Content-Type: application/json' \ |
|
-d '{ |
|
"vectors": { |
|
"size": 1536, |
|
"distance": "Cosine" |
|
}, |
|
"optimizers_config": { |
|
"default_segment_number": 2, |
|
"indexing_threshold": 20000 |
|
}, |
|
"hnsw_config": { |
|
"m": 16, |
|
"ef_construct": 100, |
|
"full_scan_threshold": 10000 |
|
} |
|
}' |
|
|
|
|
|
echo -e "\nVerifying collections:" |
|
curl -s http://localhost:6333/collections |
|
|
|
return 0 |
|
fi |
|
echo "Waiting for Qdrant to start..." |
|
sleep 1 |
|
|
|
|
|
if grep -i "error" /home/pn/.n8n/qdrant/logs/startup.log >/dev/null 2>&1; then |
|
echo "Error found in Qdrant logs:" |
|
tail -n 10 /home/pn/.n8n/qdrant/logs/startup.log |
|
fi |
|
done |
|
|
|
echo "Failed to start Qdrant server" |
|
echo "Last 10 lines of Qdrant log:" |
|
tail -n 10 /home/pn/.n8n/qdrant/logs/startup.log |
|
exit 1 |
|
} |
|
|
|
|
|
check_services() { |
|
echo "检查服务状态..." |
|
|
|
|
|
echo "Redis 状态:" |
|
redis-cli info | grep 'used_memory\|connected_clients\|total_connections_received' |
|
|
|
|
|
echo "Qdrant 状态:" |
|
if curl -s http://localhost:6333/metrics >/dev/null; then |
|
echo "Qdrant 运行正常" |
|
curl -s http://localhost:6333/metrics |
|
|
|
|
|
echo "Qdrant 集合列表:" |
|
curl -s http://localhost:6333/collections |
|
else |
|
echo "Qdrant 服务异常" |
|
tail -n 10 /home/pn/.n8n/qdrant/qdrant.log |
|
fi |
|
} |
|
|
|
|
|
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}" |
|
echo "" |
|
start_redis |
|
echo "" |
|
start_qdrant |
|
echo "" |
|
check_services |
|
|
|
|
|
source /home/pn/n8n/config/n8n_env.sh |
|
|
|
echo "" |
|
echo "Starting n8n..." |
|
exec n8n start |
|
} |
|
|
|
|
|
main "$@" |
|
|