This commit is contained in:
211
PM2_GUIDE.md
Normal file
211
PM2_GUIDE.md
Normal file
@@ -0,0 +1,211 @@
|
||||
# PM2 Process Management
|
||||
|
||||
PM2 script để quản lý tất cả các processes của Sena School Management System.
|
||||
|
||||
## Cài đặt PM2
|
||||
|
||||
```bash
|
||||
npm install -g pm2
|
||||
```
|
||||
|
||||
## Processes
|
||||
|
||||
System bao gồm các processes sau:
|
||||
|
||||
1. **sena-api** - Main API Server (2 instances, cluster mode)
|
||||
2. **worker-db-write** - Database Write Worker (1 instance)
|
||||
3. **worker-lesson-fill** - Lesson Data Fill Worker (1 instance)
|
||||
4. **worker-process-data** - Process Data Worker (2 instances)
|
||||
|
||||
## Commands
|
||||
|
||||
### Quản lý tất cả processes
|
||||
|
||||
```bash
|
||||
# Start tất cả processes
|
||||
npm run pm2:start
|
||||
|
||||
# Stop tất cả processes
|
||||
npm run pm2:stop
|
||||
|
||||
# Restart tất cả processes
|
||||
npm run pm2:restart
|
||||
|
||||
# Delete tất cả processes
|
||||
npm run pm2:delete
|
||||
|
||||
# Xem status của tất cả processes
|
||||
npm run pm2:status
|
||||
```
|
||||
|
||||
### Quản lý từng nhóm
|
||||
|
||||
```bash
|
||||
# Chỉ start API server
|
||||
npm run pm2:start:api
|
||||
|
||||
# Chỉ start workers
|
||||
npm run pm2:start:workers
|
||||
```
|
||||
|
||||
### Logs & Monitoring
|
||||
|
||||
```bash
|
||||
# Xem tất cả logs
|
||||
npm run pm2:logs
|
||||
|
||||
# Xem logs của một process cụ thể
|
||||
pm2 logs sena-api
|
||||
pm2 logs worker-process-data
|
||||
|
||||
# Xóa tất cả logs
|
||||
npm run pm2:flush
|
||||
|
||||
# Mở monitor dashboard
|
||||
npm run pm2:monit
|
||||
```
|
||||
|
||||
### Auto-start khi boot
|
||||
|
||||
```bash
|
||||
# Cấu hình PM2 khởi động cùng hệ thống
|
||||
npm run pm2:startup
|
||||
|
||||
# Sau đó chạy command mà PM2 hiển thị (với sudo nếu cần)
|
||||
# Rồi save danh sách processes
|
||||
npm run pm2:save
|
||||
```
|
||||
|
||||
### Commands nâng cao
|
||||
|
||||
```bash
|
||||
# Restart một process cụ thể
|
||||
pm2 restart sena-api
|
||||
|
||||
# Stop một process cụ thể
|
||||
pm2 stop worker-lesson-fill
|
||||
|
||||
# Xem thông tin chi tiết
|
||||
pm2 describe sena-api
|
||||
|
||||
# Xem metrics
|
||||
pm2 monit
|
||||
|
||||
# Scale API instances
|
||||
pm2 scale sena-api 4
|
||||
```
|
||||
|
||||
## File cấu hình
|
||||
|
||||
### ecosystem.config.js
|
||||
|
||||
File cấu hình PM2 chính, định nghĩa:
|
||||
- Tên processes
|
||||
- Script path
|
||||
- Số lượng instances
|
||||
- Environment variables
|
||||
- Log files
|
||||
- Memory limits
|
||||
- Restart policies
|
||||
|
||||
### Log Files
|
||||
|
||||
Logs được lưu trong thư mục `logs/`:
|
||||
|
||||
```
|
||||
logs/
|
||||
├── api-error.log
|
||||
├── api-out.log
|
||||
├── worker-db-write-error.log
|
||||
├── worker-db-write-out.log
|
||||
├── worker-lesson-fill-error.log
|
||||
├── worker-lesson-fill-out.log
|
||||
├── worker-process-data-error.log
|
||||
└── worker-process-data-out.log
|
||||
```
|
||||
|
||||
## Production Deployment
|
||||
|
||||
### 1. Install PM2 globally
|
||||
```bash
|
||||
npm install -g pm2
|
||||
```
|
||||
|
||||
### 2. Start all processes
|
||||
```bash
|
||||
npm run pm2:start
|
||||
```
|
||||
|
||||
### 3. Save process list
|
||||
```bash
|
||||
npm run pm2:save
|
||||
```
|
||||
|
||||
### 4. Configure auto-start
|
||||
```bash
|
||||
npm run pm2:startup
|
||||
# Chạy command được hiển thị
|
||||
npm run pm2:save
|
||||
```
|
||||
|
||||
### 5. Monitor
|
||||
```bash
|
||||
npm run pm2:status
|
||||
npm run pm2:logs
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Process không start
|
||||
```bash
|
||||
# Xem logs để biết lỗi
|
||||
pm2 logs <process-name>
|
||||
|
||||
# Xem thông tin chi tiết
|
||||
pm2 describe <process-name>
|
||||
```
|
||||
|
||||
### Memory leak
|
||||
```bash
|
||||
# Processes tự động restart khi vượt quá giới hạn memory
|
||||
# API: 1GB
|
||||
# Workers: 512MB
|
||||
```
|
||||
|
||||
### Restart processes sau khi update code
|
||||
```bash
|
||||
npm run pm2:restart
|
||||
```
|
||||
|
||||
### Xóa và start lại từ đầu
|
||||
```bash
|
||||
npm run pm2:delete
|
||||
npm run pm2:start
|
||||
```
|
||||
|
||||
## Environment Variables
|
||||
|
||||
Thay đổi environment trong [ecosystem.config.js](ecosystem.config.js):
|
||||
|
||||
```javascript
|
||||
env: {
|
||||
NODE_ENV: 'production',
|
||||
PORT: 3000,
|
||||
},
|
||||
env_development: {
|
||||
NODE_ENV: 'development',
|
||||
PORT: 3000,
|
||||
}
|
||||
```
|
||||
|
||||
Chạy với environment cụ thể:
|
||||
```bash
|
||||
pm2 start ecosystem.config.js --env development
|
||||
pm2 start ecosystem.config.js --env production
|
||||
```
|
||||
|
||||
## Help
|
||||
|
||||
```bash
|
||||
npm run pm2:help
|
||||
```
|
||||
Reference in New Issue
Block a user