From 48f82b55f86c847d3aed69604a58eb13c4114f1d Mon Sep 17 00:00:00 2001 From: LouisLam Date: Fri, 23 Jul 2021 12:58:05 +0800 Subject: [PATCH] prevent unexpected error throw from checkCertificate interrupt the beat --- server/model/monitor.js | 15 ++++++++++++--- server/util.js | 6 ++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/server/model/monitor.js b/server/model/monitor.js index a2a904b10..49cc0f0e9 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -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"); @@ -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 { @@ -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() { @@ -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) { diff --git a/server/util.js b/server/util.js index 0a8877b80..0282a0276 100644 --- a/server/util.js +++ b/server/util.js @@ -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) + } +} +