-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalarmExercise.js
93 lines (66 loc) · 2.6 KB
/
alarmExercise.js
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
/*
Suppose we have a web application that runs on a single server. Errors that
occur from time to time during normal operation of the application are logged to a
text file that is stored in the file system on the server.
We are not concerned about these errors when their frequency is low. However,
when lots of errors occur in a short period of time, there may be a problem with
the application and we want to be notified immediately. Specifically, when more
than ten errors occur in one minute, we want to receive an email notification.
In general terms or pseudo code, how would you implement such an alarm?
Please assume:
1. The general paradigm of logging errors to a text file will be kept in place.
2. There exists a function
function logError( error )
This function is called each time there is an error and appends the error to
the end of the log file.
3. We never want to receive more than one email notification per minute.
*/
// IMPORTANT : This is a pseudo code as an example, was not tested
const cron = require("node-cron");
const myLoggers = require('log4js');
const express = require("express");
let nodemailer = require("nodemailer");
var counter = 0;
app = express();
app.listen("3128");
/* ------------------------------ Configure Logger File ------------------------------ */
myLoggers.configure({
appenders: { mylogger: { type:"file", filename: "path_to_file/filename" } },
categories: { default: { appenders:["mylogger"], level:"ALL" } }
});
const logger = myLoggers.getLogger("default");
/* ------------------------------ Configure Email Sender ------------------------------ */
let transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: "[email protected]",
pass: "userpass"
}
});
/* ------------------------------ Configure Log Error ------------------------------ */
function logError( error ){
counter ++;
logger.warn('Error' + error);
}
/* ------------------------------ Configure Cron ------------------------------*/
cron.schedule("1 * * * *", function(){
console.log("---------------------");
console.log("Running Cron Job");
let mailOptions = {
from: "[email protected]",
to: "[email protected]",
subject: `Error in {System}`,
text: `Hi there, this email was automatically sent by us`
};
// If 10 errors accumulated within one minute, send an email.
if(counter > 10)
transporter.sendMail(mailOptions, function(error, info) {
if (error) {
throw error;
} else {
console.log("Email successfully sent!");
}
});
// Once the cron is over, I reset the counter to 0
counter = 0;
});