Commit 0485b523 authored by CompileNix's avatar CompileNix

limit notificationQueueWorker to one delivery per second

parent 7c1d2193
......@@ -14,8 +14,8 @@ class Config {
'test1.txt'
];
this.expressions = [
{ match: /HTTP\/\d\.\d\" 404/g, subject: 'HTTP 404', matchCounter: 0 },
{ match: /HTTP\/\d\.\d\" 5\d{2}/g, subject: 'HTTP 5xx', matchCounter: 0 }
{ match: /HTTP\/\d\.\d\" 404/g, subject: 'HTTP 404: ' },
{ match: /HTTP\/\d\.\d\" 5\d{2}/g, subject: 'HTTP 5xx: ' }
];
this.enableSlack = false;
this.slackWebHookUri = 'https://hooks.slack.com/services/xxxxxx/xxxxxx/xxxxxx';
......
......@@ -12,6 +12,7 @@ let mailOptions = {};
let lineCounter = 0;
const fileWatchers = {};
let messages = new Array();
let queueWorkerRunning = false;
function setupSmtp() {
transporter = nodemailer.createTransport({
......@@ -36,18 +37,28 @@ function setupSlack() {
slack.setWebhook(config.slackWebHookUri);
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function notificationQueueWorker() {
if (queueWorkerRunning) return;
queueWorkerRunning = true;
while (messages.length > 0) {
let message = messages[0];
messages.shift();
if (!message) break;
if (!message) {
continue;
}
if (config.enableEmail) {
await sendMail({
from: config.mailfrom,
to: config.mailto,
subject: `${config.subjectPrefix}`,
text: `${message.expression.subject}`
text: `${message.expression.subject}${message.message}`
});
}
......@@ -55,12 +66,16 @@ async function notificationQueueWorker() {
slack.webhook({
channel: config.slackChannel,
username: config.slackUserName,
text: `${message.expression.subject}: ${message.message}`
text: `${message.expression.subject}${message.message}`
}, (err, response) => {
if (config.debug) console.log(response);
if (config.debug && err) console.log(err, response);
});
}
await sleep(1000);
}
queueWorkerRunning = false;
}
async function sendMail(mailOptions) {
......@@ -100,12 +115,21 @@ async function filterLog( /** @type {string} */ line) {
expression: expression,
message: line
});
// notificationQueueWorker(expression, line);
}
}
}
async function setupTail( /** @type {string[]} */ filesToWatch) {
if (!config.expressions || config.expressions.length < 1) {
console.error('no expressions defined in config!');
process.exit(1);
}
for (let index = 0; index < config.expressions.length; index++) {
config.expressions[index].matchCounter = 0;
config.expressions[index].lastMatchContent = '';
}
for (const fileName of filesToWatch) {
if (!(await fs.exists(fileName))) {
console.log(`File not found, not watching: ${fileName}`);
......@@ -136,5 +160,5 @@ async function setupTail( /** @type {string[]} */ filesToWatch) {
setupSmtp();
setupSlack();
await setupTail(config.filesToWatch);
setInterval(notificationQueueWorker, 5000);
setInterval(notificationQueueWorker, 1000);
})();
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment