-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtimer.js
More file actions
115 lines (106 loc) · 3.89 KB
/
Copy pathtimer.js
File metadata and controls
115 lines (106 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
*定时发送邮件
*/
const cronJob = require("cron").CronJob;
const fs = require('fs');
const readline = require('readline');
const nodemailer = require("nodemailer");
const mailConfig = require("./config/mailConfig.json");
// 生成邮件的内容
let getMailOptions = () => {
return new Promise((resolve, reject) => {
// 返回今日要发送的的log文件名
console.log("__dirname=> ", __dirname);
let serverLogFileName = __dirname + "/log/serverLog_" + new Date().toISOString().slice(0, 10).replace(/-/g, "") + ".log";
fs.access(serverLogFileName, fs.constants.R_OK, (error) => {
if (error) {
// 不存在该log文件的情况
fs.appendFileSync(serverLogFileName, "");
};
let rl = readline.createInterface({
input: fs.createReadStream(serverLogFileName),
terminal: false
});
// 记录失败记录的条数
let failedCount = 0;
// 拼接邮件的表单部分:表头
let htmlStr = '<p>Log文件:' + serverLogFileName + '<p><br>' +
'<table border="1"><th>日期</th><th>Hash</th><th>原因</th>';
rl.on('line', (line) => {
var lineObj = JSON.parse(line);
// 拼接邮件的表单部分:行列
if (lineObj.result.status == "failed") {
htmlStr = htmlStr +
"<tr>" +
"<td>" + lineObj.date + "</td>" +
"<td>" + lineObj.hash + "</td>" +
"<td>" + lineObj.result.msg + "</td>" +
"</tr>";
failedCount++;
}
});
rl.on('close', () => {
// 表格描述
let emailTitle = '<strong style="color: red">失败记录 : ' + failedCount + '条</strong><br>'
resolve({
// 标题
subject: (failedCount > 0 ? "Failure:上链记录" : "Success:上链记录"),
// 邮件内容
html: emailTitle + htmlStr + "</table>"
});
});
});
})
}
//定时发送邮件
const TimerSendMail = () => {
// SMTP 连接
let transport = nodemailer.createTransport({
// 主机
host: mailConfig.transport.host,
// use SSL
secureConnection: true,
// port for secure SMTP
port: mailConfig.transport.port,
auth: {
// 账号
user: mailConfig.auth.user,
pass: mailConfig.auth.pass
}
});
let job = new cronJob({
cronTime: mailConfig.cronTime,
onTick: () => {
Promise.all([getMailOptions()])
.then((mailOptions) => {
transport.sendMail({
// 发件人地址
from: mailConfig.options.from,
// 收件人地址,可以使用逗号隔开添加多个
to: mailConfig.options.to,
// 标题
subject: mailOptions[0].subject,
// 邮件内容
html: mailOptions[0].html
}, (error, response) => {
if (error) {
console.error(error);
} else {
console.log('Mail Send Ok');
}
// 关闭连接
transport.close();
})
}).catch((error) => {
console.log("error when log file");
return;
});
},
start: false,
timeZone: mailConfig.timeZone
})
job.start();
}
module.exports = {
TimerSendMail: TimerSendMail
}