library-picturebook-activity/backend/ecosystem.config.js
2026-01-18 17:58:38 +08:00

124 lines
3.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* PM2 进程管理器配置文件
*
* 环境区分说明:
* 1. 通过 --env 参数指定环境pm2 start ecosystem.config.js --env <环境名>
* 2. 环境配置会自动合并:基础配置(env) + 环境特定配置(env_<环境名>)
* 3. 测试环境: --env test (端口 3001, 2个实例)
* 4. 生产环境: --env production (端口 3000, 最大实例数)
*/
const baseAppConfig = {
script: './dist/src/main.js',
// 日志文件路径
error_file: './logs/pm2-error.log',
out_file: './logs/pm2-out.log',
log_file: './logs/pm2-combined.log',
// 日志日期格式
log_date_format: 'YYYY-MM-DD HH:mm:ss Z',
// 合并日志(所有实例的日志合并到一个文件)
merge_logs: true,
// 自动重启配置
autorestart: true,
// 监听文件变化(生产环境建议关闭)
watch: false,
// 忽略监听的文件/目录
ignore_watch: ['node_modules', 'logs', 'dist', '.git', '*.log'],
// 最大内存限制(超过后自动重启)
max_memory_restart: '1G',
// 最小正常运行时间(秒),小于此时间重启不计入重启次数
min_uptime: '10s',
// 最大重启次数(在 min_uptime 时间内)
max_restarts: 10,
// 重启延迟(毫秒)
restart_delay: 4000,
// 等待就绪信号的时间(毫秒)
wait_ready: true,
listen_timeout: 10000,
// 优雅关闭超时时间(毫秒)
kill_timeout: 5000,
// 应用启动后的等待时间(毫秒)
shutdown_with_message: true,
// 源代码映射支持
source_map_support: true,
// 实例间负载均衡策略
instance_var: 'INSTANCE_ID',
};
module.exports = {
apps: [
{
...baseAppConfig,
// 生产环境应用
name: 'competition-api',
instances: 2,
exec_mode: 'cluster',
env_file: '.env.production',
env: {
NODE_ENV: 'production',
PORT: 3234,
},
},
{
...baseAppConfig,
// 测试环境应用
name: 'competition-api-test',
instances: 2,
exec_mode: 'cluster',
env_file: '.env.test',
env: {
NODE_ENV: 'test',
PORT: 3234,
},
},
],
// ============================================
// 部署配置(用于 PM2 自动化部署)
// 使用方式: pm2 deploy ecosystem.config.js <环境名>
// ============================================
deploy: {
// 测试环境部署配置
test: {
user: 'deploy',
host: ['119.29.229.174'],
ref: 'origin/develop',
repo: 'git@github.com:your-username/competition-management-system.git',
path: '/var/www/competition-management-test',
'post-deploy':
'cd backend && pnpm install && pnpm run build && pm2 reload ecosystem.config.js --only competition-api-test',
'pre-setup': 'apt-get update && apt-get install -y git',
},
// 生产环境部署配置
production: {
user: 'deploy',
host: ['your-prod-server-ip'],
ref: 'origin/master',
repo: 'git@github.com:your-username/competition-management-system.git',
path: '/var/www/competition-management',
'post-deploy':
'cd backend && pnpm install && pnpm run build && pm2 reload ecosystem.config.js --only competition-api',
'pre-setup': 'apt-get update && apt-get install -y git',
},
},
};