Skip to content

Commit

Permalink
prevent unexpected error throw from checkCertificate interrupt the beat
Browse files Browse the repository at this point in the history
  • Loading branch information
louislam committed Jul 23, 2021
1 parent 6b72d50 commit 48f82b5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
15 changes: 12 additions & 3 deletions server/model/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var timezone = require('dayjs/plugin/timezone')
dayjs.extend(utc)
dayjs.extend(timezone)
const axios = require("axios");
const {debug} = require("../util");
const {tcping, ping, checkCertificate} = require("../util-server");
const {R} = require("redbean-node");
const {BeanModel} = require("redbean-node/dist/bean-model");
Expand Down Expand Up @@ -84,10 +85,18 @@ class Monitor extends BeanModel {
bean.ping = dayjs().valueOf() - startTime;

// Check certificate if https is used

let certInfoStartTime = dayjs().valueOf();
if (this.getUrl()?.protocol === "https:") {
await this.updateTlsInfo(checkCertificate(res));
try {
await this.updateTlsInfo(checkCertificate(res));
} catch (e) {
console.error(e.message)
}
}

debug("Cert Info Query Time: " + (dayjs().valueOf() - certInfoStartTime) + "ms")

if (this.type === "http") {
bean.status = 1;
} else {
Expand Down Expand Up @@ -178,7 +187,7 @@ class Monitor extends BeanModel {
clearInterval(this.heartbeatInterval)
}

// Helper Method:
// Helper Method:
// returns URL object for further usage
// returns null if url is invalid
getUrl() {
Expand All @@ -199,7 +208,7 @@ class Monitor extends BeanModel {
tls_info_bean.monitor_id = this.id;
}
tls_info_bean.info_json = JSON.stringify(checkCertificateResult);
R.store(tls_info_bean);
await R.store(tls_info_bean);
}

static async sendStats(io, monitorID, userID) {
Expand Down
6 changes: 6 additions & 0 deletions server/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@ exports.ucfirst = function (str) {
return firstLetter.toUpperCase() + str.substr(1);
}

exports.debug = (msg) => {
if (process.env.NODE_ENV === "development") {
console.log(msg)
}
}

0 comments on commit 48f82b5

Please sign in to comment.