Add safety check for monitor
This commit is contained in:
1 parent
d1add402b0
commit
8ac53a9e16
3 files changed
+41
-30
No files matched your search
@@ -280,6 +280,9 @@ const getMonitorStatsById = async (req) => {
|
|||||||
const getMonitorById = async (monitorId) => {
|
const getMonitorById = async (monitorId) => {
|
||||||
try {
|
try {
|
||||||
const monitor = await Monitor.findById(monitorId);
|
const monitor = await Monitor.findById(monitorId);
|
||||||
|
if (monitor === null || monitor === undefined) {
|
||||||
|
throw new Error(errorMessages.DB_FIND_MONTIOR_BY_ID(monitorId));
|
||||||
|
}
|
||||||
// Get notifications
|
// Get notifications
|
||||||
const notifications = await Notification.find({
|
const notifications = await Notification.find({
|
||||||
monitorId: monitorId,
|
monitorId: monitorId,
|
||||||
|
|||||||
+3
-3
@@ -140,9 +140,9 @@ const startApp = async () => {
|
|||||||
}
|
}
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
};
|
};
|
||||||
process.on("SIGUSR2", cleanup);
|
// process.on("SIGUSR2", cleanup);
|
||||||
process.on("SIGINT", cleanup);
|
// process.on("SIGINT", cleanup);
|
||||||
process.on("SIGTERM", cleanup);
|
// process.on("SIGTERM", cleanup);
|
||||||
};
|
};
|
||||||
|
|
||||||
startApp().catch((error) => {
|
startApp().catch((error) => {
|
||||||
|
|||||||
@@ -16,37 +16,45 @@ class NetworkService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async handleNotification(job, isAlive) {
|
async handleNotification(job, isAlive) {
|
||||||
const { _id } = job.data;
|
try {
|
||||||
const monitor = await this.db.getMonitorById(_id);
|
const { _id } = job.data;
|
||||||
if (monitor === null || monitor === undefined) {
|
const monitor = await this.db.getMonitorById(_id);
|
||||||
logger.error(`Null Monitor: ${_id}`, {
|
if (monitor === null || monitor === undefined) {
|
||||||
|
logger.error(`Null Monitor: ${_id}`, {
|
||||||
|
method: "handleNotification",
|
||||||
|
service: this.SERVICE_NAME,
|
||||||
|
jobId: job.id,
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If monitor status changes, update monitor status and send notification
|
||||||
|
if (monitor.status !== isAlive) {
|
||||||
|
monitor.status = !monitor.status;
|
||||||
|
await monitor.save();
|
||||||
|
|
||||||
|
let template =
|
||||||
|
isAlive === true ? "serverIsUpTemplate" : "serverIsDownTemplate";
|
||||||
|
let status = isAlive === true ? "up" : "down";
|
||||||
|
|
||||||
|
const notifications = await this.db.getNotificationsByMonitorId(_id);
|
||||||
|
for (const notification of notifications) {
|
||||||
|
if (notification.type === "email") {
|
||||||
|
await this.emailService.buildAndSendEmail(
|
||||||
|
template,
|
||||||
|
{ monitorName: monitor.name, monitorUrl: monitor.url },
|
||||||
|
notification.address,
|
||||||
|
`Monitor ${monitor.name} is ${status}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(error.message, {
|
||||||
method: "handleNotification",
|
method: "handleNotification",
|
||||||
service: this.SERVICE_NAME,
|
service: this.SERVICE_NAME,
|
||||||
jobId: job.id,
|
jobId: job.id,
|
||||||
});
|
});
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If monitor status changes, update monitor status and send notification
|
|
||||||
if (monitor.status !== isAlive) {
|
|
||||||
monitor.status = !monitor.status;
|
|
||||||
await monitor.save();
|
|
||||||
|
|
||||||
let template =
|
|
||||||
isAlive === true ? "serverIsUpTemplate" : "serverIsDownTemplate";
|
|
||||||
let status = isAlive === true ? "up" : "down";
|
|
||||||
|
|
||||||
const notifications = await this.db.getNotificationsByMonitorId(_id);
|
|
||||||
for (const notification of notifications) {
|
|
||||||
if (notification.type === "email") {
|
|
||||||
await this.emailService.buildAndSendEmail(
|
|
||||||
template,
|
|
||||||
{ monitorName: monitor.name, monitorUrl: monitor.url },
|
|
||||||
notification.address,
|
|
||||||
`Monitor ${monitor.name} is ${status}`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user