restore netowrk service

This commit is contained in:
Alex Holliday committed 2024-09-03 15:45:11 -07:00
1 parent 5af81c7291
commit c490722eae
1 file changed
+49 -33
+49 -33
View File
@@ -15,44 +15,60 @@ class NetworkService {
this.NETWORK_ERROR = 5000; this.NETWORK_ERROR = 5000;
} }
async handleNotification(job, isAlive) { async handleNotification(monitor, isAlive) {
try { try {
const { _id } = job.data; let template =
const monitor = await this.db.getMonitorById(_id); isAlive === true ? "serverIsUpTemplate" : "serverIsDownTemplate";
if (monitor === null || monitor === undefined) { let status = isAlive === true ? "up" : "down";
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 const notifications = await this.db.getNotificationsByMonitorId(
if (monitor.status !== isAlive) { monitor._id
monitor.status = !monitor.status; );
await monitor.save(); for (const notification of notifications) {
if (notification.type === "email") {
let template = await this.emailService.buildAndSendEmail(
isAlive === true ? "serverIsUpTemplate" : "serverIsDownTemplate"; template,
let status = isAlive === true ? "up" : "down"; { monitorName: monitor.name, monitorUrl: monitor.url },
notification.address,
const notifications = await this.db.getNotificationsByMonitorId(_id); `Monitor ${monitor.name} is ${status}`
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) { } catch (error) {
logger.error(error.message, { logger.error(error.message, {
method: "handleNotification", method: "handleNotification",
service: this.SERVICE_NAME, service: this.SERVICE_NAME,
monitorId: monitor._id,
});
}
}
async handleStatusUpdate(job, isAlive) {
try {
const { _id } = job.data;
const monitor = await this.db.getMonitorById(_id);
if (monitor === null || monitor === undefined) {
logger.error(`Null Monitor: ${_id}`, {
method: "handleStatusUpdate",
service: this.SERVICE_NAME,
jobId: job.id,
});
return;
}
if (monitor.status === undefined || monitor.status !== isAlive) {
const oldStatus = monitor.status;
monitor.status = isAlive;
await monitor.save();
if (oldStatus !== undefined && oldStatus !== isAlive) {
this.handleNotification(monitor, isAlive);
}
}
} catch (error) {
logger.error(error.message, {
method: "handleStatusUpdate",
service: this.SERVICE_NAME,
jobId: job.id, jobId: job.id,
}); });
} }
@@ -113,7 +129,7 @@ class NetworkService {
}; };
return await this.logAndStoreCheck(checkData, this.db.createCheck); return await this.logAndStoreCheck(checkData, this.db.createCheck);
} finally { } finally {
this.handleNotification(job, isAlive); this.handleStatusUpdate(job, isAlive);
} }
} }
@@ -161,7 +177,7 @@ class NetworkService {
return await this.logAndStoreCheck(checkData, this.db.createCheck); return await this.logAndStoreCheck(checkData, this.db.createCheck);
} finally { } finally {
this.handleNotification(job, isAlive); this.handleStatusUpdate(job, isAlive);
} }
} }
@@ -241,7 +257,7 @@ class NetworkService {
}; };
this.logAndStoreCheck(checkData, this.db.createPageSpeedCheck); this.logAndStoreCheck(checkData, this.db.createPageSpeedCheck);
} finally { } finally {
this.handleNotification(job, isAlive); this.handleStatusUpdate(job, isAlive);
} }
} }