加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
pm2Start.js 2.06 KB
一键复制 编辑 原始数据 按行查看 历史
huluoyang 提交于 2016-04-09 07:07 . modify email and remove .git
require('dotenv').load();
var pm2 = require('pm2');
var nodemailer = require('nodemailer');
var moment = require('moment-timezone');
var _ = require('lodash');
var instances = process.env.INSTANCES || 1;
var serverName = process.env.SERVER_NAME || 'server';
var maxMemory = process.env.MAX_MEMORY || '390M';
var transportOptions = {
type: 'smtp',
service: 'Mandrill',
auth: {
user: process.env.MANDRILL_USER || false,
pass: process.env.MANDRILL_PASSWORD
}
};
var mailReceiver = process.env.MAIL_RECEIVER || false;
pm2.connect(function() {
pm2.start({
name: serverName,
script: 'server/production-start.js',
'exec_mode': 'cluster',
instances: instances,
'max_memory_restart': maxMemory,
'NODE_ENV': 'production'
}, function() {
console.log(
'pm2 started %s with %s instances at %s max memory',
serverName,
instances,
maxMemory
);
pm2.disconnect();
});
});
if (transportOptions.auth.user && mailReceiver) {
console.log('setting up mailer');
var transporter = nodemailer.createTransport(transportOptions);
var compiled = _.template(
'An error has occurred on server ' +
'<% name %>\n' +
'Stack Trace:\n\n\n<%= stack %>\n\n\n' +
'Context:\n\n<%= text %>'
);
pm2.launchBus(function(err, bus) {
if (err) {
return console.error(err);
}
console.log('event bus connected');
bus.on('process:exception', function(data) {
var text;
var stack;
var name;
try {
data.date = moment(data.at || new Date())
.tz('America/Los_Angeles')
.format('MMMM Do YYYY, h:mm:ss a z');
text = JSON.stringify(data, null, 2);
stack = data.data.stack;
name = data.process.name;
} catch (e) {
return e;
}
transporter.sendMail({
to: mailReceiver,
// from: 'team@freecodecamp.com',
// subject: 'Server exception',
from: 'huluoyang@freecodecamp.cn',
subject: 'Server exception',
text: compiled({ name: name, text: text, stack: stack })
});
});
});
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化