gallyga commited on
Commit
12fa81e
·
verified ·
1 Parent(s): 672cb2e

Create run.sh

Browse files
Files changed (1) hide show
  1. run.sh +277 -0
run.sh ADDED
@@ -0,0 +1,277 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+ set -eo pipefail
3
+
4
+ # 导入环境变量
5
+ source /home/pn/.env
6
+
7
+ # 错误处理函数
8
+ handle_error() {
9
+ echo "错误发生在第 $1 行"
10
+ exit 1
11
+ }
12
+ trap 'handle_error $LINENO' ERR
13
+
14
+ # 超时处理函数
15
+ timeout_handler() {
16
+ echo "操作超时"
17
+ exit 1
18
+ }
19
+
20
+ # 等待服务就绪的通用函数
21
+ wait_for_service() {
22
+ local service=$1
23
+ local host=$2
24
+ local port=$3
25
+ local timeout=${4:-$WAIT_TIMEOUT}
26
+
27
+ echo "等待 $service 就绪..."
28
+ local end=$((SECONDS + timeout))
29
+
30
+ while [ $SECONDS -lt $end ]; do
31
+ if nc -z "$host" "$port" >/dev/null 2>&1; then
32
+ echo "$service 已就绪"
33
+ return 0
34
+ fi
35
+ echo "尝试连接 $service at $host:$port..."
36
+ sleep 1
37
+ done
38
+
39
+ echo "$service 启动超时"
40
+ exit 1
41
+ }
42
+
43
+ # 启动 Redis 服务
44
+ start_redis() {
45
+ echo "Starting Redis server..."
46
+ redis-server --daemonize yes
47
+ sleep 1
48
+ if ! redis-cli ping > /dev/null 2>&1; then
49
+ echo "Failed to start Redis server"
50
+ exit 1
51
+ fi
52
+
53
+ # 设置 Redis 配置
54
+ redis-cli config set maxmemory 512mb
55
+ redis-cli config set maxmemory-policy allkeys-lru
56
+ echo ""
57
+ echo "Redis server started successfully"
58
+ echo ""
59
+ }
60
+
61
+ # 启动 Qdrant 服务
62
+ start_qdrant() {
63
+ echo "Starting Qdrant server..."
64
+
65
+ # 确保目录存在并有正确的权限
66
+ mkdir -p /home/pn/.n8n/qdrant/storage
67
+ mkdir -p /home/pn/.n8n/qdrant/config
68
+ mkdir -p /home/pn/.n8n/qdrant/snapshots
69
+ mkdir -p /home/pn/.n8n/qdrant/logs
70
+
71
+ # 设置正确的权限
72
+ chmod -R 755 /home/pn/.n8n/qdrant
73
+ chown -R pn:pn /home/pn/.n8n/qdrant
74
+
75
+ # 创建 Qdrant 配置文件
76
+ cat > /home/pn/.n8n/qdrant/config/config.yaml <<EOF
77
+ service:
78
+ host: 0.0.0.0
79
+ http_port: 6333
80
+ grpc_port: 6334
81
+ enable_cors: true
82
+ enable_tls: false
83
+ max_request_size_mb: 64
84
+ max_workers: 0
85
+ storage:
86
+ storage_path: /home/pn/.n8n/qdrant/storage
87
+ snapshots_path: /home/pn/.n8n/qdrant/snapshots
88
+ on_disk_payload: true
89
+
90
+ performance:
91
+ max_search_threads: 0
92
+ max_optimization_threads: 0
93
+
94
+ optimizers:
95
+ deleted_threshold: 0.2
96
+ vacuum_min_vector_number: 1000
97
+ default_segment_number: 0
98
+ max_segment_size_kb: null
99
+ indexing_threshold_kb: 20000
100
+ flush_interval_sec: 5
101
+
102
+ hnsw_index:
103
+ m: 16
104
+ ef_construct: 100
105
+ full_scan_threshold_kb: 10000
106
+ max_indexing_threads: 0
107
+ on_disk: false
108
+ logger:
109
+ on_disk:
110
+ enabled: true
111
+ log_file: /home/pn/.n8n/qdrant/logs/qdrant.log
112
+ log_level: INFO
113
+ telemetry_disabled: true
114
+ EOF
115
+
116
+ # 确保配置文件有正确的权限
117
+ chmod 644 /home/pn/.n8n/qdrant/config/config.yaml
118
+
119
+ # 使用配置文件启动 Qdrant
120
+ qdrant --config-path /home/pn/.n8n/qdrant/config/config.yaml > /home/pn/.n8n/qdrant/logs/startup.log 2>&1 &
121
+
122
+ # 等待 Qdrant 启动
123
+ local timeout=30
124
+ local end=$((SECONDS + timeout))
125
+
126
+ while [ $SECONDS -lt $end ]; do
127
+ if curl -s http://localhost:6333/health >/dev/null; then
128
+ echo "Qdrant server started successfully"
129
+
130
+ # 预创建常用集合
131
+ echo "Creating default collections..."
132
+
133
+ # 创建文本向量集合 (768维,适用于多数文本嵌入模型)
134
+ curl -X PUT 'http://localhost:6333/collections/text_vectors' \
135
+ -H 'Content-Type: application/json' \
136
+ -d '{
137
+ "vectors": {
138
+ "size": 768,
139
+ "distance": "Cosine",
140
+ "on_disk": true
141
+ },
142
+ "optimizers_config": {
143
+ "default_segment_number": 2,
144
+ "indexing_threshold": 20000,
145
+ "memmap_threshold": 10000
146
+ },
147
+ "hnsw_config": {
148
+ "m": 16,
149
+ "ef_construct": 100,
150
+ "full_scan_threshold": 10000,
151
+ "max_indexing_threads": 0,
152
+ "on_disk": true
153
+ },
154
+ "init_from": {
155
+ "collection_name": "text_vectors"
156
+ }
157
+ }'
158
+
159
+ # 创建图像向量集合 (512维,适用于多数图像嵌入模型)
160
+ curl -X PUT 'http://localhost:6333/collections/image_vectors' \
161
+ -H 'Content-Type: application/json' \
162
+ -d '{
163
+ "vectors": {
164
+ "size": 512,
165
+ "distance": "Cosine"
166
+ },
167
+ "optimizers_config": {
168
+ "default_segment_number": 2,
169
+ "indexing_threshold": 20000
170
+ },
171
+ "hnsw_config": {
172
+ "m": 16,
173
+ "ef_construct": 100,
174
+ "full_scan_threshold": 10000
175
+ }
176
+ }'
177
+
178
+ # 创建通用向量集合 (1536维,适用于 OpenAI 的嵌入模型)
179
+ curl -X PUT 'http://localhost:6333/collections/openai_vectors' \
180
+ -H 'Content-Type: application/json' \
181
+ -d '{
182
+ "vectors": {
183
+ "size": 1536,
184
+ "distance": "Cosine"
185
+ },
186
+ "optimizers_config": {
187
+ "default_segment_number": 2,
188
+ "indexing_threshold": 20000
189
+ },
190
+ "hnsw_config": {
191
+ "m": 16,
192
+ "ef_construct": 100,
193
+ "full_scan_threshold": 10000
194
+ }
195
+ }'
196
+
197
+ # 验证集合创建状态并输出详细信息
198
+ echo -e "\nVerifying collections:"
199
+ curl -s 'http://localhost:6333/collections' | jq '.'
200
+
201
+ # 测试连接
202
+ echo -e "\nTesting Qdrant connection:"
203
+ curl -v http://localhost:6333/health
204
+
205
+ return 0
206
+ fi
207
+ echo "Waiting for Qdrant to start..."
208
+ sleep 1
209
+
210
+ # 检查是否有错误日志
211
+ if grep -i "error" /home/pn/.n8n/qdrant/logs/startup.log >/dev/null 2>&1; then
212
+ echo "Error found in Qdrant logs:"
213
+ tail -n 10 /home/pn/.n8n/qdrant/logs/startup.log
214
+ fi
215
+ done
216
+
217
+ echo "Failed to start Qdrant server"
218
+ echo "Last 10 lines of Qdrant log:"
219
+ tail -n 10 /home/pn/.n8n/qdrant/logs/startup.log
220
+ exit 1
221
+ }
222
+
223
+ # 检查服务状态
224
+ check_services() {
225
+ echo "检查服务状态..."
226
+
227
+ # 检查 Redis
228
+ echo "Redis 状态:"
229
+ redis-cli info | grep 'used_memory\|connected_clients\|total_connections_received'
230
+
231
+ # 检查 Qdrant
232
+ echo "Qdrant 状态:"
233
+ if curl -s http://localhost:6333/metrics >/dev/null; then
234
+ echo "Qdrant 运行正常"
235
+ curl -s http://localhost:6333/metrics
236
+
237
+ # 显示集合信息
238
+ echo "Qdrant 集合列表:"
239
+ curl -s http://localhost:6333/collections
240
+ else
241
+ echo "Qdrant 服务异常"
242
+ tail -n 10 /home/pn/.n8n/qdrant/qdrant.log
243
+ fi
244
+ }
245
+
246
+ # 主流程
247
+ main() {
248
+ current_time=$(date +"%Y-%m-%d %H:%M:%S")
249
+ echo "Starting services at $current_time"
250
+
251
+ # 输出配置信息
252
+ echo "Database Configuration:"
253
+ echo "Host: ${DB_POSTGRESDB_HOST}"
254
+ echo "Port: ${DB_POSTGRESDB_PORT}"
255
+ echo "User: ${DB_POSTGRESDB_USER}"
256
+ echo "Database: ${DB_POSTGRESDB_DATABASE}"
257
+ echo "Type: ${DB_TYPE}"
258
+
259
+ # 启动服务
260
+ wait_for_service "PostgreSQL" "${DB_POSTGRESDB_HOST}" "${DB_POSTGRESDB_PORT}"
261
+ echo ""
262
+ start_redis
263
+ echo ""
264
+ start_qdrant
265
+ echo ""
266
+ check_services
267
+
268
+ # 设置 N8N 环境变量
269
+ source /home/pn/n8n/config/n8n_env.sh
270
+
271
+ echo ""
272
+ echo "Starting n8n..."
273
+ exec n8n start
274
+ }
275
+
276
+ # 执行主流程
277
+ main "$@"