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