Move all notification handling from JobQueue to NotificaitonService where it should be
This commit is contained in:
1 parent
257cc73ab3
commit
02666397c9
2 files changed
+80
-45
No files matched your search
@@ -165,26 +165,12 @@ class JobQueue {
|
|||||||
// Handle status change
|
// Handle status change
|
||||||
const { monitor, statusChanged, prevStatus } =
|
const { monitor, statusChanged, prevStatus } =
|
||||||
await this.statusService.updateStatus(networkResponse);
|
await this.statusService.updateStatus(networkResponse);
|
||||||
|
// Handle notifications
|
||||||
// If this is an infrastructure monitor, we need to check for thresholds
|
|
||||||
if (monitor.type === "hardware") {
|
|
||||||
await this.notificationService.handleInfrastructureNotifications({
|
|
||||||
...networkResponse,
|
|
||||||
monitor,
|
|
||||||
prevStatus,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
//If status hasn't changed, we're done
|
|
||||||
if (statusChanged === false) return;
|
|
||||||
|
|
||||||
// if prevStatus is undefined, monitor is resuming, we're done
|
|
||||||
if (prevStatus === undefined) return;
|
|
||||||
|
|
||||||
this.notificationService.handleNotifications({
|
this.notificationService.handleNotifications({
|
||||||
...networkResponse,
|
...networkResponse,
|
||||||
monitor,
|
monitor,
|
||||||
prevStatus,
|
prevStatus,
|
||||||
|
statusChanged,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.error({
|
this.logger.error({
|
||||||
|
|||||||
@@ -14,48 +14,62 @@ class NotificationService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends an email notification based on the network response.
|
* Sends an email notification for hardware infrastructure alerts
|
||||||
*
|
*
|
||||||
* @param {Object} networkResponse - The response from the network monitor.
|
* @async
|
||||||
* @param {Object} networkResponse.monitor - The monitor object containing details about the monitored service.
|
* @function sendHardwareEmail
|
||||||
* @param {string} networkResponse.monitor.name - The name of the monitor.
|
* @param {Object} networkResponse - Response object containing monitor information
|
||||||
* @param {string} networkResponse.monitor.url - The URL of the monitor.
|
* @param {string} address - Email address to send the notification to
|
||||||
* @param {boolean} networkResponse.status - The current status of the monitor (true for up, false for down).
|
* @param {Array} [alerts=[]] - List of hardware alerts to include in the email
|
||||||
* @param {boolean} networkResponse.prevStatus - The previous status of the monitor (true for up, false for down).
|
* @returns {Promise<boolean>} - Indicates whether email was sent successfully
|
||||||
* @param {string} address - The email address to send the notification to.
|
* @throws {Error}
|
||||||
*/
|
*/
|
||||||
async sendEmail(networkResponse, address, alerts = []) {
|
async sendHardwareEmail(networkResponse, address, alerts = []) {
|
||||||
|
if (alerts.length === 0) return false;
|
||||||
|
const { monitor, status, prevStatus } = networkResponse;
|
||||||
|
const template = "hardwareIncidentTemplate";
|
||||||
|
const context = { monitor: monitor.name, url: monitor.url, alerts };
|
||||||
|
const subject = `Monitor ${monitor.name} infrastructure alerts`;
|
||||||
|
this.emailService.buildAndSendEmail(template, context, address, subject);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends an email notification about monitor status change
|
||||||
|
*
|
||||||
|
* @async
|
||||||
|
* @function sendEmail
|
||||||
|
* @param {Object} networkResponse - Response object containing monitor status information
|
||||||
|
* @param {string} address - Email address to send the notification to
|
||||||
|
* @returns {Promise<boolean>} - Indicates email was sent successfully
|
||||||
|
*/
|
||||||
|
async sendEmail(networkResponse, address) {
|
||||||
const { monitor, status, prevStatus } = networkResponse;
|
const { monitor, status, prevStatus } = networkResponse;
|
||||||
if (monitor.type === "hardware") {
|
|
||||||
const template = "hardwareIncidentTemplate";
|
|
||||||
const context = { monitor: monitor.name, url: monitor.url, alerts };
|
|
||||||
const subject = `Monitor ${monitor.name} infrastructure alerts`;
|
|
||||||
this.emailService.buildAndSendEmail(template, context, address, subject);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const template = prevStatus === false ? "serverIsUpTemplate" : "serverIsDownTemplate";
|
const template = prevStatus === false ? "serverIsUpTemplate" : "serverIsDownTemplate";
|
||||||
const context = { monitor: monitor.name, url: monitor.url };
|
const context = { monitor: monitor.name, url: monitor.url };
|
||||||
const subject = `Monitor ${monitor.name} is ${status === true ? "up" : "down"}`;
|
const subject = `Monitor ${monitor.name} is ${status === true ? "up" : "down"}`;
|
||||||
this.emailService.buildAndSendEmail(template, context, address, subject);
|
this.emailService.buildAndSendEmail(template, context, address, subject);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
async handleStatusNotifications(networkResponse) {
|
||||||
* Handles notifications based on the network response.
|
|
||||||
*
|
|
||||||
* @param {Object} networkResponse - The response from the network monitor.
|
|
||||||
* @param {string} networkResponse.monitorId - The ID of the monitor.
|
|
||||||
*/
|
|
||||||
async handleNotifications(networkResponse) {
|
|
||||||
try {
|
try {
|
||||||
|
//If status hasn't changed, we're done
|
||||||
|
if (networkResponse.statusChanged === false) return false;
|
||||||
|
|
||||||
|
// if prevStatus is undefined, monitor is resuming, we're done
|
||||||
|
if (networkResponse.prevStatus === undefined) return false;
|
||||||
const notifications = await this.db.getNotificationsByMonitorId(
|
const notifications = await this.db.getNotificationsByMonitorId(
|
||||||
networkResponse.monitorId
|
networkResponse.monitorId
|
||||||
);
|
);
|
||||||
|
|
||||||
for (const notification of notifications) {
|
for (const notification of notifications) {
|
||||||
if (notification.type === "email") {
|
if (notification.type === "email") {
|
||||||
this.sendEmail(networkResponse, notification.address);
|
this.sendEmail(networkResponse, notification.address);
|
||||||
}
|
}
|
||||||
// Handle other types of notifications here
|
// Handle other types of notifications here
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.warn({
|
this.logger.warn({
|
||||||
message: error.message,
|
message: error.message,
|
||||||
@@ -65,10 +79,18 @@ class NotificationService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
async handleInfrastructureNotifications(networkResponse) {
|
* Handles status change notifications for a monitor
|
||||||
|
*
|
||||||
|
* @async
|
||||||
|
* @function handleStatusNotifications
|
||||||
|
* @param {Object} networkResponse - Response object containing monitor status information
|
||||||
|
* @returns {Promise<boolean>} - Indicates whether notifications were processed
|
||||||
|
* @throws {Error}
|
||||||
|
*/
|
||||||
|
async handleHardwareNotifications(networkResponse) {
|
||||||
const thresholds = networkResponse?.monitor?.thresholds;
|
const thresholds = networkResponse?.monitor?.thresholds;
|
||||||
if (thresholds === undefined) return; // No thresholds set, we're done
|
if (thresholds === undefined) return false; // No thresholds set, we're done
|
||||||
|
|
||||||
// Get thresholds from monitor
|
// Get thresholds from monitor
|
||||||
const {
|
const {
|
||||||
@@ -78,7 +100,9 @@ class NotificationService {
|
|||||||
} = thresholds;
|
} = thresholds;
|
||||||
|
|
||||||
// Get metrics from response
|
// Get metrics from response
|
||||||
const metrics = networkResponse?.payload?.data ?? {};
|
const metrics = networkResponse?.payload?.data ?? null;
|
||||||
|
if (metrics === null) return false;
|
||||||
|
|
||||||
const {
|
const {
|
||||||
cpu: { usage_percent: cpuUsage = -1 } = {},
|
cpu: { usage_percent: cpuUsage = -1 } = {},
|
||||||
memory: { usage_percent: memoryUsage = -1 } = {},
|
memory: { usage_percent: memoryUsage = -1 } = {},
|
||||||
@@ -121,7 +145,6 @@ class NotificationService {
|
|||||||
", "
|
", "
|
||||||
)} is above your threshold (${(diskThreshold * 100).toFixed(0)}%)`,
|
)} is above your threshold (${(diskThreshold * 100).toFixed(0)}%)`,
|
||||||
};
|
};
|
||||||
|
|
||||||
alertsToSend.push(formatAlert[type]());
|
alertsToSend.push(formatAlert[type]());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -132,9 +155,35 @@ class NotificationService {
|
|||||||
if (alertsToSend.length === 0) continue; // No alerts to send, we're done
|
if (alertsToSend.length === 0) continue; // No alerts to send, we're done
|
||||||
|
|
||||||
if (notification.type === "email") {
|
if (notification.type === "email") {
|
||||||
this.sendEmail(networkResponse, notification.address, alertsToSend);
|
this.sendHardwareEmail(networkResponse, notification.address, alertsToSend);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles notifications for different monitor types
|
||||||
|
*
|
||||||
|
* @async
|
||||||
|
* @function handleNotifications
|
||||||
|
* @param {Object} networkResponse - Response object containing monitor information
|
||||||
|
* @returns {Promise<boolean>} - Indicates whether notifications were processed successfully
|
||||||
|
*/
|
||||||
|
async handleNotifications(networkResponse) {
|
||||||
|
try {
|
||||||
|
if (networkResponse.monitor.type === "hardware") {
|
||||||
|
this.handleHardwareNotifications(networkResponse);
|
||||||
|
}
|
||||||
|
this.handleStatusNotifications(networkResponse);
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.warn({
|
||||||
|
message: error.message,
|
||||||
|
service: this.SERVICE_NAME,
|
||||||
|
method: "handleNotifications",
|
||||||
|
stack: error.stack,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user