mirror of
https://github.com/bluewave-labs/Checkmate.git
synced 2026-05-12 12:39:05 -05:00
Minor refactor for correctness
This commit is contained in:
@@ -98,7 +98,7 @@ class JobQueue {
|
||||
}, false);
|
||||
|
||||
if (!maintenanceWindowActive) {
|
||||
const res = await this.networkService.getStatus(job);
|
||||
await this.networkService.getStatus(job);
|
||||
} else {
|
||||
logger.info(`Monitor ${monitorId} is in maintenance window`, {
|
||||
service: SERVICE_NAME,
|
||||
|
||||
@@ -55,7 +55,7 @@ class NetworkService {
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error(error.message, {
|
||||
this.logger.error(error.message, {
|
||||
method: "handleNotification",
|
||||
service: this.SERVICE_NAME,
|
||||
monitorId: monitor._id,
|
||||
@@ -72,9 +72,10 @@ class NetworkService {
|
||||
*/
|
||||
async handleStatusUpdate(job, isAlive) {
|
||||
let monitor;
|
||||
const { _id } = job.data;
|
||||
|
||||
// Look up the monitor, if it doesn't exist, it's probably been removed, return
|
||||
try {
|
||||
const { _id } = job.data;
|
||||
monitor = await this.db.getMonitorById(_id);
|
||||
} catch (error) {
|
||||
return;
|
||||
@@ -83,7 +84,7 @@ class NetworkService {
|
||||
// Otherwise, try to update monitor status
|
||||
try {
|
||||
if (monitor === null || monitor === undefined) {
|
||||
logger.error(`Null Monitor: ${_id}`, {
|
||||
this.logger.error(`Null Monitor: ${_id}`, {
|
||||
method: "handleStatusUpdate",
|
||||
service: this.SERVICE_NAME,
|
||||
jobId: job.id,
|
||||
@@ -100,7 +101,7 @@ class NetworkService {
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error(error.message, {
|
||||
this.logger.error(error.message, {
|
||||
method: "handleStatusUpdate",
|
||||
service: this.SERVICE_NAME,
|
||||
jobId: job.id,
|
||||
@@ -123,7 +124,7 @@ class NetworkService {
|
||||
} catch (error) {
|
||||
const endTime = Date.now();
|
||||
error.responseTime = endTime - startTime;
|
||||
error.service === undefined ? (error.service = SERVICE_NAME) : null;
|
||||
error.service === undefined ? (error.service = this.SERVICE_NAME) : null;
|
||||
error.method === undefined ? (error.method = "measureResponseTime") : null;
|
||||
throw error;
|
||||
}
|
||||
@@ -135,15 +136,12 @@ class NetworkService {
|
||||
* @returns {Promise<{boolean}} The result of logging and storing the check
|
||||
*/
|
||||
async handlePing(job) {
|
||||
const operation = async () => {
|
||||
const response = await this.ping.promise.probe(job.data.url);
|
||||
return response;
|
||||
};
|
||||
|
||||
let isAlive;
|
||||
|
||||
try {
|
||||
const { responseTime, response } = await this.measureResponseTime(operation);
|
||||
const { responseTime, response } = await this.measureResponseTime(
|
||||
this.ping.promise.probe(job.data.url)
|
||||
);
|
||||
isAlive = response.alive;
|
||||
const checkData = {
|
||||
monitorId: job.data._id,
|
||||
@@ -153,7 +151,7 @@ class NetworkService {
|
||||
? successMessages.PING_SUCCESS
|
||||
: errorMessages.PING_CANNOT_RESOLVE,
|
||||
};
|
||||
return await this.logAndStoreCheck(checkData, this.db.createCheck);
|
||||
await this.logAndStoreCheck(checkData, this.db.createCheck);
|
||||
} catch (error) {
|
||||
isAlive = false;
|
||||
const checkData = {
|
||||
@@ -162,7 +160,7 @@ class NetworkService {
|
||||
message: errorMessages.PING_CANNOT_RESOLVE,
|
||||
responseTime: error.responseTime,
|
||||
};
|
||||
return await this.logAndStoreCheck(checkData, this.db.createCheck);
|
||||
await this.logAndStoreCheck(checkData, this.db.createCheck);
|
||||
} finally {
|
||||
this.handleStatusUpdate(job, isAlive);
|
||||
}
|
||||
@@ -196,7 +194,7 @@ class NetworkService {
|
||||
statusCode: response.status,
|
||||
message: this.http.STATUS_CODES[response.status],
|
||||
};
|
||||
return await this.logAndStoreCheck(checkData, this.db.createCheck);
|
||||
await this.logAndStoreCheck(checkData, this.db.createCheck);
|
||||
} catch (error) {
|
||||
const statusCode = error.response?.status || this.NETWORK_ERROR;
|
||||
let message = this.http.STATUS_CODES[statusCode] || "Network Error";
|
||||
@@ -208,8 +206,6 @@ class NetworkService {
|
||||
responseTime: error.responseTime,
|
||||
message,
|
||||
};
|
||||
|
||||
return await this.logAndStoreCheck(checkData, this.db.createCheck);
|
||||
} finally {
|
||||
this.handleStatusUpdate(job, isAlive);
|
||||
}
|
||||
@@ -233,6 +229,7 @@ class NetworkService {
|
||||
let isAlive;
|
||||
try {
|
||||
const url = job.data.url;
|
||||
|
||||
const response = await this.axios.get(
|
||||
`https://pagespeedonline.googleapis.com/pagespeedonline/v5/runPagespeed?url=${url}&category=seo&category=accessibility&category=best-practices&category=performance`
|
||||
);
|
||||
@@ -246,7 +243,6 @@ class NetworkService {
|
||||
"largest-contentful-paint": lcp,
|
||||
"total-blocking-time": tbt,
|
||||
} = audits;
|
||||
|
||||
// Weights
|
||||
// First Contentful Paint 10%
|
||||
// Speed Index 10%
|
||||
@@ -272,7 +268,6 @@ class NetworkService {
|
||||
tbt,
|
||||
},
|
||||
};
|
||||
|
||||
this.logAndStoreCheck(checkData, this.db.createPageSpeedCheck);
|
||||
} catch (error) {
|
||||
isAlive = false;
|
||||
@@ -334,8 +329,9 @@ class NetworkService {
|
||||
if (insertedCheck !== null && insertedCheck !== undefined) {
|
||||
return insertedCheck.status;
|
||||
}
|
||||
throw new Error();
|
||||
} catch (error) {
|
||||
this.logger.error(`Error wrtiting check for ${data.monitorId}`, {
|
||||
this.logger.error(`Error writing check for ${data.monitorId}`, {
|
||||
service: this.SERVICE_NAME,
|
||||
method: "logAndStoreCheck",
|
||||
monitorId: data.monitorId,
|
||||
|
||||
Reference in New Issue
Block a user