From 0f440596c897ad11ff186b56bbf38f7e278a6216 Mon Sep 17 00:00:00 2001 From: LouisLam Date: Wed, 8 Sep 2021 19:54:37 +0800 Subject: [PATCH] Revert "cache last heartbeat list in memory" This reverts commit 87678ea92d52b502dfb8d5eeb3f2b06f4d2765ed. --- server/client.js | 15 ++++++------ server/limit-array.js | 13 ---------- server/model/monitor.js | 49 +++---------------------------------- server/server.js | 32 ++++++++++++------------ server/user-monitor-list.js | 22 ----------------- 5 files changed, 26 insertions(+), 105 deletions(-) delete mode 100644 server/limit-array.js diff --git a/server/client.js b/server/client.js index d75f2900..fcfddeb5 100644 --- a/server/client.js +++ b/server/client.js @@ -33,14 +33,13 @@ async function sendNotificationList(socket) { async function sendHeartbeatList(socket, monitorID, toUser = false, overwrite = false) { const timeLogger = new TimeLogger(); - let monitor = userMonitorList.getMonitor(socket.userID, monitorID); - - if (! monitor) { - console.error("No this monitor??"); - return; - } - - let list = monitor.getCachedHeartbeatList().array; + let list = await R.find("heartbeat", ` + monitor_id = ? + ORDER BY time DESC + LIMIT 100 + `, [ + monitorID, + ]) let result = []; diff --git a/server/limit-array.js b/server/limit-array.js deleted file mode 100644 index e89b116b..00000000 --- a/server/limit-array.js +++ /dev/null @@ -1,13 +0,0 @@ -class LimitArray { - limit = 100; - array = []; - - add(item) { - this.array.push(item); - if (this.array.length > this.limit) { - this.array.shift(); - } - } -} - -module.exports = LimitArray; diff --git a/server/model/monitor.js b/server/model/monitor.js index 3b3959ff..57815cdd 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -6,20 +6,14 @@ dayjs.extend(utc) dayjs.extend(timezone) const axios = require("axios"); const { Prometheus } = require("../prometheus"); -const { debug, UP, DOWN, PENDING, flipStatus, TimeLogger, getRandomInt } = require("../../src/util"); +const { debug, UP, DOWN, PENDING, flipStatus, TimeLogger } = require("../../src/util"); const { tcping, ping, dnsResolve, checkCertificate, checkStatusCode, getTotalClientInRoom } = require("../util-server"); const { R } = require("redbean-node"); const { BeanModel } = require("redbean-node/dist/bean-model"); const { Notification } = require("../notification") const { userMonitorList } = require("../user-monitor-list"); -const LimitArray = require("../limit-array"); const version = require("../../package.json").version; -/** - * Master List - */ -const cachedHeartbeatList = {}; - /** * status: * 0 = DOWN @@ -27,29 +21,6 @@ const cachedHeartbeatList = {}; * 2 = PENDING */ class Monitor extends BeanModel { - - /** - * Cache Last 100 Heartbeat list - * @returns {LimitArray} - */ - getCachedHeartbeatList() { - return cachedHeartbeatList[this.id]; - } - - async onOpen() { - this.beanMeta.extraData = {}; - - cachedHeartbeatList[this.id] = new LimitArray(); - - cachedHeartbeatList[this.id].array = await R.find("heartbeat", ` - monitor_id = ? - ORDER BY time DESC - LIMIT 100 - `, [ - this.id, - ]); - } - async toJSON() { let notificationIDList = {}; @@ -112,6 +83,7 @@ class Monitor extends BeanModel { let prometheus = new Prometheus(this); const beat = async () => { + // Expose here for prometheus update // undefined if not https let tlsInfo = undefined; @@ -330,37 +302,22 @@ class Monitor extends BeanModel { console.warn(`Monitor #${this.id} '${this.name}': Failing: ${bean.msg} | Type: ${this.type}`) } - if (! this.beanMeta.extraData.uniqueNumber) { - this.beanMeta.extraData.uniqueNumber = getRandomInt(0, 9999999999); - } - - debug(this.beanMeta.extraData.uniqueNumber); - io.to(this.user_id).emit("heartbeat", bean.toJSON()); Monitor.sendStats(io, this.id, this.user_id) await R.store(bean); - - // Also add to cache - this.getCachedHeartbeatList().add(bean); - prometheus.update(bean, tlsInfo); previousBeat = bean; - if (this.beanMeta.extraData.stop !== true) { - this.heartbeatInterval = setTimeout(beat, this.interval * 1000); - } + this.heartbeatInterval = setTimeout(beat, this.interval * 1000); } beat(); } stop() { - console.log(`[Monitor: ${this.id}] Stop`); - debug("Stop " + this.beanMeta.extraData.uniqueNumber) clearTimeout(this.heartbeatInterval); - this.beanMeta.extraData.stop = true; } /** diff --git a/server/server.js b/server/server.js index 8d61c7cd..126aa18d 100644 --- a/server/server.js +++ b/server/server.js @@ -283,7 +283,8 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); bean.import(monitor) bean.user_id = socket.userID - await R.store(bean); + await R.store(bean) + await updateMonitorNotification(bean.id, notificationIDList) await startMonitor(socket.userID, bean.id); @@ -426,6 +427,7 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); let monitor = userMonitorList.getMonitor(socket.userID, monitorID); if (monitor) { + monitor.stop(); userMonitorList.delete(socket.userID, monitorID); } @@ -676,7 +678,7 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); }); console.log("Starting All Monitors"); - await initMonitors(); + await startMonitors(); console.log("Init the server"); @@ -816,18 +818,24 @@ async function startMonitor(userID, monitorID) { let monitor = await R.findOne("monitor", " id = ? ", [ monitorID, - ]); + ]) + + let oldMonitor = userMonitorList.getMonitor(userID, monitorID); + + if (oldMonitor) { + oldMonitor.stop(); + } userMonitorList.add(userID, monitor); monitor.start(io) } async function restartMonitor(userID, monitorID) { - return await startMonitor(userID, monitorID); + return await startMonitor(userID, monitorID) } async function pauseMonitor(userID, monitorID) { - await checkOwner(userID, monitorID); + await checkOwner(userID, monitorID) console.log(`Pause Monitor: ${monitorID} User ID: ${userID}`) @@ -839,7 +847,6 @@ async function pauseMonitor(userID, monitorID) { let monitor = userMonitorList.getMonitor(userID, monitorID); if (monitor) { - monitor.active = 0; monitor.stop(); } } @@ -847,21 +854,14 @@ async function pauseMonitor(userID, monitorID) { /** * Resume active monitors */ -async function initMonitors() { - // Init all monitors - let list = await R.find("monitor"); - let activeList = []; +async function startMonitors() { + let list = await R.find("monitor", " active = 1 "); for (let monitor of list) { userMonitorList.add(monitor.user_id, monitor); - - if (monitor.active) { - activeList.push(monitor); - } } - // Start active monitors only - delayStartMonitors(activeList); + delayStartMonitors(list); } /** diff --git a/server/user-monitor-list.js b/server/user-monitor-list.js index 0ce64fb4..053060bd 100644 --- a/server/user-monitor-list.js +++ b/server/user-monitor-list.js @@ -4,36 +4,14 @@ class UserMonitorList { list = {}; - /** - * Add or update - * @param userID - * @param monitor - */ add(userID, monitor) { if (! this.list[userID]) { this.list[userID] = {}; } - - // Stopped the old monitor if same id - this.stop(userID, monitor.id); - this.list[userID][monitor.id] = monitor; } - stop(userID, monitorID) { - if (this.list[userID][monitorID]) { - let oldMonitor = this.list[userID][monitorID]; - - if (oldMonitor) { - oldMonitor.stop(); - } else { - console.log("No old monitor: " + monitorID); - } - } - } - delete(userID, monitorID) { - this.stop(userID, monitorID); let monitorList = this.getMonitorList(userID); delete monitorList[monitorID]; }