forked from NoFxAiOS/nofx
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathpm2.sh
More file actions
executable file
·258 lines (228 loc) · 6 KB
/
pm2.sh
File metadata and controls
executable file
·258 lines (228 loc) · 6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/bin/bash
# NoFX Trading Bot - PM2 管理脚本
# 用法: ./pm2.sh [start|stop|restart|status|logs|build]
set -e
# 自动获取脚本所在目录(支持符号链接)
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$PROJECT_ROOT"
# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
# 函数:打印带颜色的消息
print_info() {
echo -e "${BLUE}ℹ️ $1${NC}"
}
print_success() {
echo -e "${GREEN}✅ $1${NC}"
}
print_warning() {
echo -e "${YELLOW}⚠️ $1${NC}"
}
print_error() {
echo -e "${RED}❌ $1${NC}"
}
print_header() {
echo -e "${PURPLE}═══════════════════════════════════════${NC}"
echo -e "${PURPLE} 🤖 NoFX Trading Bot - PM2 Manager${NC}"
echo -e "${PURPLE}═══════════════════════════════════════${NC}"
echo ""
}
# 函数:检查 PM2 是否安装
check_pm2() {
if ! command -v pm2 &> /dev/null; then
print_error "PM2 未安装,请先安装: npm install -g pm2"
exit 1
fi
}
# 函数:确保日志目录存在
ensure_log_dirs() {
mkdir -p "$PROJECT_ROOT/logs"
mkdir -p "$PROJECT_ROOT/web/logs"
print_info "日志目录已创建"
}
# 函数:编译后端
build_backend() {
print_info "正在编译后端..."
go build -o nofx
if [ $? -eq 0 ]; then
print_success "后端编译完成"
else
print_error "后端编译失败"
exit 1
fi
}
# 函数:构建前端(生产环境)
build_frontend() {
print_info "正在构建前端..."
cd web
npm run build
if [ $? -eq 0 ]; then
print_success "前端构建完成"
cd ..
else
print_error "前端构建失败"
exit 1
fi
}
# 函数:启动服务
start_services() {
print_header
ensure_log_dirs
# 检查后端二进制文件是否存在
if [ ! -f "./nofx" ]; then
print_warning "后端二进制文件不存在,开始编译..."
build_backend
fi
print_info "正在启动服务..."
pm2 start pm2.config.js
sleep 2
pm2 status
echo ""
print_success "服务启动完成!"
echo ""
echo -e "${CYAN}📊 访问地址:${NC}"
echo -e " ${GREEN}前端:${NC} http://localhost:3000"
echo -e " ${GREEN}后端 API:${NC} http://localhost:8080"
echo ""
echo -e "${CYAN}📝 查看日志:${NC}"
echo -e " ${GREEN}实时日志:${NC} ./pm2.sh logs"
echo -e " ${GREEN}后端日志:${NC} ./pm2.sh logs backend"
echo -e " ${GREEN}前端日志:${NC} ./pm2.sh logs frontend"
echo ""
}
# 函数:停止服务
stop_services() {
print_header
print_info "正在停止服务..."
pm2 stop pm2.config.js
print_success "服务已停止"
}
# 函数:重启服务
restart_services() {
print_header
print_info "正在重启服务..."
pm2 restart pm2.config.js
sleep 2
pm2 status
print_success "服务已重启"
}
# 函数:删除服务
delete_services() {
print_header
print_warning "正在删除 PM2 服务..."
pm2 delete pm2.config.js || true
print_success "PM2 服务已删除"
}
# 函数:查看状态
show_status() {
print_header
pm2 status
echo ""
print_info "详细信息:"
pm2 info nofx-backend
echo ""
pm2 info nofx-frontend
}
# 函数:查看日志
show_logs() {
if [ -z "$2" ]; then
# 显示所有日志
pm2 logs
elif [ "$2" = "backend" ]; then
pm2 logs nofx-backend
elif [ "$2" = "frontend" ]; then
pm2 logs nofx-frontend
else
print_error "未知的日志类型: $2"
print_info "用法: ./pm2.sh logs [backend|frontend]"
exit 1
fi
}
# 函数:监控
show_monitor() {
print_header
print_info "启动 PM2 监控面板..."
pm2 monit
}
# 函数:重新编译并重启
rebuild_and_restart() {
print_header
print_info "正在重新编译后端..."
build_backend
print_info "正在重启后端服务..."
pm2 restart nofx-backend
sleep 2
pm2 status
print_success "后端已重新编译并重启"
}
# 函数:显示帮助
show_help() {
print_header
echo -e "${CYAN}使用方法:${NC}"
echo " ./pm2.sh [command]"
echo ""
echo -e "${CYAN}可用命令:${NC}"
echo -e " ${GREEN}start${NC} - 启动前后端服务"
echo -e " ${GREEN}stop${NC} - 停止所有服务"
echo -e " ${GREEN}restart${NC} - 重启所有服务"
echo -e " ${GREEN}status${NC} - 查看服务状态"
echo -e " ${GREEN}logs${NC} - 查看所有日志 (Ctrl+C 退出)"
echo -e " ${GREEN}logs backend${NC} - 查看后端日志"
echo -e " ${GREEN}logs frontend${NC} - 查看前端日志"
echo -e " ${GREEN}monitor${NC} - 打开 PM2 监控面板"
echo -e " ${GREEN}build${NC} - 编译后端"
echo -e " ${GREEN}rebuild${NC} - 重新编译后端并重启"
echo -e " ${GREEN}delete${NC} - 删除 PM2 服务"
echo -e " ${GREEN}help${NC} - 显示此帮助信息"
echo ""
echo -e "${CYAN}示例:${NC}"
echo " ./pm2.sh start # 启动服务"
echo " ./pm2.sh logs backend # 查看后端日志"
echo " ./pm2.sh rebuild # 重新编译后端并重启"
echo ""
}
# 主逻辑
check_pm2
case "${1:-help}" in
start)
start_services
;;
stop)
stop_services
;;
restart)
restart_services
;;
status)
show_status
;;
logs)
show_logs "$@"
;;
monitor|mon)
show_monitor
;;
build)
build_backend
;;
rebuild)
rebuild_and_restart
;;
delete|remove)
delete_services
;;
help|--help|-h)
show_help
;;
*)
print_error "未知命令: $1"
echo ""
show_help
exit 1
;;
esac