HJ_Server/js_pm2.config.js
2023-12-19 17:00:25 +08:00

112 lines
3.7 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.

const fs = require('fs');
const os = require('os');
const path = require('path');
const {env} = require('process');
const request = require('sync-request');
console.log('服务端环境变量 ', env.SERVER_ID, env.SERVER_TYPE);
if (env.SERVER_ID) {
let type = env.SERVER_TYPE || 'game';
let url = `http://server-tool:7456/server/getConfig?sid=${env.SERVER_ID}&type=${type}`
console.log('拉取服务端启动配置文件', url);
let res = request('GET', url, {
headers: {
'content-type': 'application/json'
}
});
const body = JSON.parse(res.body.toString());
if (body) {
fs.writeFileSync('config.json', JSON.stringify(body, null, 2));
console.log('服务端配置: ', body);
} else {
console.log('拉取失败');
}
}
//读取服务配置
const config = fs.existsSync(path.resolve(__dirname, 'config.json')) ? JSON.parse(fs.readFileSync(path.resolve(__dirname, 'config.json'), 'utf-8')) : {};
const dis = config.serverId != undefined ? config.serverId : '0';
let instancesNum = 8;
if(config.openTime){
let openDate = new Date(config.openTime);
//计算openTime距离今天的天数
let today = new Date();
// 获取两个日期之间的毫秒差
let timeDiff = today.getTime() - openDate.getTime();
// 将毫秒差转换为天数
let daysDiff = Math.floor(timeDiff / (1000 * 3600 * 24));
console.log(`已开服天数:${daysDiff}`);
//如果开服超过3天的区则只开区2个进程
if(daysDiff > 3){
instancesNum = 2;
}
}
const localApps = [
{
// 应用程序名称
name: 'heijiao_msg_s' + dis,
// 执行文件
script: 'index.js',
// 应用程序所在的目录
cwd: './',
//集群实例数量0表示根据cpu核心数自动控制
instances: instancesNum,
// 传递给脚本的参数
args: '-serverType msg',
//允许强制gc
node_args: '--expose-gc',
// 是否启用监控模式默认是false。如果设置成true当应用程序变动时pm2会自动重载。这里也可以设置你要监控的文件。
watch: false,
// 不用监听的文件
ignore_watch: [
'logs'
],
//out_file: "/dev/stdout",
//error_file: "/dev/stderr"
// 自定义应用程序的错误日志文件(错误日志文件)
error_file: './logs/errMsg.log',
// 自定义应用程序日志文件(正常日志文件)
out_file: './logs/outMsg.log',
// 指定日志文件的时间格式
log_date_format: 'YYYY-MM-DD HH:mm:ss',
}
];
const crossApps = [
{
// 应用程序名称
name: 'heijiao_cross_s' + dis,
// 执行文件
script: 'index.js',
// 应用程序所在的目录
cwd: './',
// 传递给脚本的参数
args: '-serverType cross',
//允许强制gc
node_args: '--expose-gc',
// 是否启用监控模式默认是false。如果设置成true当应用程序变动时pm2会自动重载。这里也可以设置你要监控的文件。
watch: false,
// 不用监听的文件
ignore_watch: [
'logs'
],
//out_file: "/dev/stdout",
//error_file: "/dev/stderr"
// 自定义应用程序的错误日志文件(错误日志文件)
error_file: './logs/errCross.log',
// 自定义应用程序日志文件(正常日志文件)
out_file: './logs/outCross.log',
// 指定日志文件的时间格式
log_date_format: 'YYYY-MM-DD HH:mm:ss',
}
]
module.exports = {
apps: config.type == 'cross' ? crossApps : localApps
}