Merge pull request #150 from bluewave-labs/feat/checks
Checks now inserted into db during monitor status check, reslovles #141
This commit is contained in:
4 files changed
+69
-41
No files matched your search
@@ -162,9 +162,9 @@ const deleteMonitor = async (req, res, next) => {
|
|||||||
try {
|
try {
|
||||||
const monitor = await req.db.deleteMonitor(req, res, next);
|
const monitor = await req.db.deleteMonitor(req, res, next);
|
||||||
// Delete associated checks and alerts
|
// Delete associated checks and alerts
|
||||||
console.log(monitor);
|
await req.jobQueue.deleteJob(monitor);
|
||||||
req.jobQueue.deleteJob(monitor);
|
await req.db.deleteChecks(monitor._id);
|
||||||
|
await req.db.deleteAlertByMonitorId(monitor._id);
|
||||||
/**
|
/**
|
||||||
* TODO
|
* TODO
|
||||||
* We should remove all checks and alerts associated with this monitor
|
* We should remove all checks and alerts associated with this monitor
|
||||||
|
|||||||
+3
-13
@@ -249,14 +249,12 @@ const deleteMonitor = async (req, res) => {
|
|||||||
const monitorId = req.params.monitorId;
|
const monitorId = req.params.monitorId;
|
||||||
try {
|
try {
|
||||||
const monitor = await Monitor.findByIdAndDelete(monitorId);
|
const monitor = await Monitor.findByIdAndDelete(monitorId);
|
||||||
deleteChecks(monitorId);
|
|
||||||
deleteAlertByMonitorId(monitorId);
|
|
||||||
if (!monitor) {
|
if (!monitor) {
|
||||||
throw new Error(errorMessages.DB_FIND_MONTIOR_BY_ID(monitorId));
|
throw new Error(errorMessages.DB_FIND_MONTIOR_BY_ID(monitorId));
|
||||||
}
|
}
|
||||||
|
|
||||||
return monitor;
|
return monitor;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.log("CATHCING DELETE MONITOR ERROR");
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -340,11 +338,7 @@ const getChecks = async (monitorId) => {
|
|||||||
const deleteChecks = async (monitorId) => {
|
const deleteChecks = async (monitorId) => {
|
||||||
try {
|
try {
|
||||||
const result = await Check.deleteMany({ monitorId });
|
const result = await Check.deleteMany({ monitorId });
|
||||||
if (result.deletedCount > 0) {
|
return result.deletedCount;
|
||||||
return result.deletedCount;
|
|
||||||
} else {
|
|
||||||
throw new Error(errorMessages.DB_DELETE_CHECKS(monitorId));
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
@@ -461,11 +455,7 @@ const editAlert = async (alertId, alertData) => {
|
|||||||
const deleteAlert = async (alertId) => {
|
const deleteAlert = async (alertId) => {
|
||||||
try {
|
try {
|
||||||
const result = await Alert.findByIdAndDelete(alertId);
|
const result = await Alert.findByIdAndDelete(alertId);
|
||||||
if (result) {
|
return result;
|
||||||
return result;
|
|
||||||
} else {
|
|
||||||
throw new Error(errorMessages.DB_DELETE_ALERT(alertId));
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ class JobQueue {
|
|||||||
QUEUE_NAME,
|
QUEUE_NAME,
|
||||||
async (job) => {
|
async (job) => {
|
||||||
try {
|
try {
|
||||||
await this.networkService.getStatus(job);
|
const res = await this.networkService.getStatus(job);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(`Error processing job ${job.id}: ${error.message}`, {
|
logger.error(`Error processing job ${job.id}: ${error.message}`, {
|
||||||
service: SERVICE_NAME,
|
service: SERVICE_NAME,
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
const axios = require("axios");
|
const axios = require("axios");
|
||||||
const ping = require("ping");
|
const ping = require("ping");
|
||||||
const logger = require("../utils/logger");
|
const logger = require("../utils/logger");
|
||||||
|
const Check = require("../models/Check");
|
||||||
|
|
||||||
class NetworkService {
|
class NetworkService {
|
||||||
constructor(db) {
|
constructor(db) {
|
||||||
@@ -10,6 +11,12 @@ class NetworkService {
|
|||||||
this.SERVICE_NAME = "NetworkService";
|
this.SERVICE_NAME = "NetworkService";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Measures the response time of an asynchronous operation.
|
||||||
|
* @param {Function} operation - An asynchronous operation to measure.
|
||||||
|
* @returns {Promise<{responseTime: number, response: any}>} An object containing the response time in milliseconds and the response from the operation.
|
||||||
|
* @throws {Error} The error object from the operation, contains response time.
|
||||||
|
*/
|
||||||
async measureResponseTime(operation) {
|
async measureResponseTime(operation) {
|
||||||
const startTime = Date.now();
|
const startTime = Date.now();
|
||||||
try {
|
try {
|
||||||
@@ -17,10 +24,17 @@ class NetworkService {
|
|||||||
const endTime = Date.now();
|
const endTime = Date.now();
|
||||||
return { responseTime: endTime - startTime, response };
|
return { responseTime: endTime - startTime, response };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
const endTime = Date.now();
|
||||||
|
error.responseTime = endTime - startTime;
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the ping operation for a given job, measures its response time, and logs the result.
|
||||||
|
* @param {Object} job - The job object containing data for the ping operation.
|
||||||
|
* @returns {Promise<{boolean}} The result of logging and storing the check
|
||||||
|
*/
|
||||||
async handlePing(job) {
|
async handlePing(job) {
|
||||||
const operation = async () => {
|
const operation = async () => {
|
||||||
const response = await ping.promise.probe(job.data.url);
|
const response = await ping.promise.probe(job.data.url);
|
||||||
@@ -32,14 +46,17 @@ class NetworkService {
|
|||||||
operation
|
operation
|
||||||
);
|
);
|
||||||
const isAlive = response.alive;
|
const isAlive = response.alive;
|
||||||
await this.logAndStoreCheck(job, isAlive, responseTime);
|
return await this.logAndStoreCheck(job, isAlive, responseTime);
|
||||||
return isAlive;
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
await this.logAndStoreCheck(job, false, 0, error);
|
return await this.logAndStoreCheck(job, false, error.responseTime, error);
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the http operation for a given job, measures its response time, and logs the result.
|
||||||
|
* @param {Object} job - The job object containing data for the ping operation.
|
||||||
|
* @returns {Promise<{boolean}} The result of logging and storing the check
|
||||||
|
*/
|
||||||
async handleHttp(job) {
|
async handleHttp(job) {
|
||||||
const operation = async () => {
|
const operation = async () => {
|
||||||
const response = await axios.get(job.data.url);
|
const response = await axios.get(job.data.url);
|
||||||
@@ -50,14 +67,18 @@ class NetworkService {
|
|||||||
operation
|
operation
|
||||||
);
|
);
|
||||||
const isAlive = response.status >= 200 && response.status < 300;
|
const isAlive = response.status >= 200 && response.status < 300;
|
||||||
await this.logAndStoreCheck(job, isAlive, responseTime);
|
return await this.logAndStoreCheck(job, isAlive, responseTime);
|
||||||
return isAlive;
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
await this.logAndStoreCheck(job, false, 0, error);
|
return await this.logAndStoreCheck(job, false, error.responseTime, error);
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Retrieves the status of a given job based on its type.
|
||||||
|
* For unsupported job types, it logs an error and returns false.
|
||||||
|
*
|
||||||
|
* @param {Object} job - The job object containing data necessary for processing.
|
||||||
|
* @returns {Promise<boolean>} The status of the job if it is supported and processed successfully, otherwise false.
|
||||||
|
*/
|
||||||
async getStatus(job) {
|
async getStatus(job) {
|
||||||
switch (job.data.type) {
|
switch (job.data.type) {
|
||||||
case this.TYPE_PING:
|
case this.TYPE_PING:
|
||||||
@@ -65,30 +86,47 @@ class NetworkService {
|
|||||||
case this.TYPE_HTTP:
|
case this.TYPE_HTTP:
|
||||||
return await this.handleHttp(job);
|
return await this.handleHttp(job);
|
||||||
default:
|
default:
|
||||||
console.error(`Unsupported type: ${job.data.type}`, {
|
logger.error(`Unsupported type: ${job.data.type}`, {
|
||||||
service: this.SERVICE_NAME,
|
service: this.SERVICE_NAME,
|
||||||
timestamp: new Date().toISOString(),
|
jobId: job.id,
|
||||||
});
|
});
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Logs and stores the result of a check for a specific job.
|
||||||
|
* This function creates a new Check object with the job's details and the result of the check,
|
||||||
|
* then attempts to save this object to the database. If the save operation is successful,
|
||||||
|
* it returns the status of the inserted check. If an error occurs during the save operation,
|
||||||
|
* it logs the error and returns false.
|
||||||
|
*
|
||||||
|
* @param {Object} job - The job object containing data necessary for the check.
|
||||||
|
* @param {boolean} isAlive - The result of the check, indicating if the target is alive.
|
||||||
|
* @param {number} responseTime - The response time measured during the check.
|
||||||
|
* @param {Error} [error=null] - Optional error object if an error occurred during the check.
|
||||||
|
* @returns {Promise<boolean>} The status of the inserted check if successful, otherwise false.
|
||||||
|
*/
|
||||||
async logAndStoreCheck(job, isAlive, responseTime, error = null) {
|
async logAndStoreCheck(job, isAlive, responseTime, error = null) {
|
||||||
const status = isAlive ? "alive" : "dead";
|
const check = new Check({
|
||||||
if (error) {
|
monitorId: job.data._id,
|
||||||
console.log(`Job ${job.data.url} is dead`);
|
status: isAlive,
|
||||||
return;
|
responseTime,
|
||||||
}
|
statusCode: 0,
|
||||||
|
message: error ? error.message : "",
|
||||||
|
});
|
||||||
|
|
||||||
if (isAlive) {
|
try {
|
||||||
console.log(
|
const insertedCheck = await check.save();
|
||||||
`Job ${job.data.url} is alive, response time: ${responseTime}ms`
|
return insertedCheck.status;
|
||||||
);
|
} catch (error) {
|
||||||
return;
|
logger.error(`Error wrtiting check for ${job.id}`, {
|
||||||
|
service: this.SERVICE_NAME,
|
||||||
|
jobId: job.id,
|
||||||
|
error: error,
|
||||||
|
});
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`Job ${job.data.url} is dead`);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user