var http = require('http');
var nodemailer=require('nodemailer');
var checkfile="http://127.0.0.1:8081/assets/css/main.css"; //请求工程的静态文件,用于判断工程是否挂掉
var erroroption={
from:'server<xxxxx@qq.com>',
to:'xxxxx@qq.com',
cc:'xxxxx@qq.com,xxxxxx@qq.com',
subject:"工程出错",
text:"服务器挂掉了,请尽快处理。"
};
var restartoption={
from:'server<xxxxx@qq.com>',
to:'xxxxx@qq.com',
cc:'xxxxx@qq.com,xxxxxx@qq.com',
subject:"工程恢复",
text:"服务器已恢复。"
};
var mail=nodemailer.createTransport({
host: "smtp.qq.com",
server:'QQ',
port:465,
secureConnection:true,
auth:{
user:'xxxxxxx@qq.com',
pass:'xxxxxxxx'
}
});
var restart=true;//判断程序是否已启动
var timer = setInterval(function () {
http.get(checkfile, function (res) {
if(!restart){
mail.sendMail(restartoption, function (error, info) {
var date=new Date();
console.log(date+'工程已恢复 ');
restart = true;
});
}
}).on('error', function (e) {
if(restart) {
mail.sendMail(erroroption, function (error, info) {
var date=new Date();
console.log(date+'工程挂掉了');
restart = false;
});
}
});
}, 60*1000);//检测频率 60S一次
最近用nodemailer写了一个监控工程的功能。主要是实现工程挂掉的时候,发邮件提醒,然后工程重启的时候再发邮件通知。但是遇到了一些问题,我这里设定了每次挂掉或者重启只发一次邮件,但是有时候会连续发几封,这是为什么?还有有时候callback都执行了,但是邮件却收不到。
Sending emails is very slow, and the server still needs to respond after execution, which is normal.