#!/bin/bash set -eo pipefail # Import environment variables #source /home/pn/.env # Error handling function handle_error() { echo "Error Line $1" exit 1 } trap 'handle_error $LINENO' ERR # Timeout processing function timeout_handler() { echo "Timeout" exit 1 } # Generic function to wait for service readiness wait_for_service() { local service=$1 local host=$2 local port=$3 local timeout=${4:-$WAIT_TIMEOUT} echo "Waiting for $service to be ready..." local end=$((SECONDS + timeout)) while [ $SECONDS -lt $end ]; do if nc -z "$host" "$port" >/dev/null 2>&1; then echo "$service is ready" return 0 fi echo "Trying to connect to $service at $host:$port..." sleep 1 done echo "$service startup timed out" exit 1 } # Start Qdrant service start_qdrant() { echo "Starting Qdrant server..." # Ensure directories exist and have correct permissions echo "Create Qdrant folder" 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 # Set correct permissions chmod -R 755 /home/pn/.n8n/qdrant chown -R pn:pn /home/pn/.n8n/qdrant # Create Qdrant config file echo "Create Qdrant config" cat > /home/pn/.n8n/qdrant/config/config.yaml < /home/pn/.n8n/qdrant/logs/startup.log 2>&1 & # Wait for Qdrant to start 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" # Create default collections echo "Creating default collections..." # Create text vector collection (768 dimensions, suitable for most text embedding models) curl -X PUT 'http://localhost:6333/collections/text_vectors' \ -H 'Content-Type: application/json' \ -d '{ "vectors": { "size": 1024, "distance": "Cosine", "on_disk": true }, "optimizers_config": { "default_segment_number": 2, "indexing_threshold": 20000, "memmap_threshold": 10000 }, "hnsw_config": { "m": 16, "ef_construct": 100, "full_scan_threshold": 10000, "max_indexing_threads": 0, "on_disk": true } }' # Create image vector collection (512 dimensions, suitable for most image embedding models) 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 } }' # Create generic vector collection (1536 dimensions, suitable for OpenAI embedding models) 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 } }' # Verify collection creation status and output details echo -e "\nVerifying collections:" curl -s 'http://localhost:6333/collections' | jq '.' # Test connection echo -e "\nTesting Qdrant connection:" curl -v http://localhost:6333/health return 0 fi echo "Waiting for Qdrant to start..." sleep 1 # Check for error logs 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 service status check_services() { echo "Checking service status..." # Check Qdrant echo "Qdrant status:" if curl -s http://localhost:6333/metrics >/dev/null; then echo "Qdrant is running normally" curl -s http://localhost:6333/metrics # Display collection information echo "Qdrant collection list:" curl -s http://localhost:6333/collections else echo "Qdrant service is abnormal" tail -n 10 /home/pn/.n8n/qdrant/qdrant.log fi } # Main process main() { current_time=$(date +"%Y-%m-%d %H:%M:%S") echo "Starting services at $current_time" # Output configuration information # 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}" # Start service # wait_for_service "PostgreSQL" "${DB_POSTGRESDB_HOST}" "${DB_POSTGRESDB_PORT}" # echo "" # start_redis # echo "" start_qdrant echo "" check_services # Set N8N environment variables source /home/pn/n8n/config/n8n_env.sh echo "" echo "Starting n8n..." exec n8n start } # Execute the main process main "$@"