Revert "cache last heartbeat list in memory"

This reverts commit 87678ea92d.
This commit is contained in:
LouisLam 2021-09-08 19:54:37 +08:00
parent 5a0fcebd6e
commit 0f440596c8
5 changed files with 26 additions and 105 deletions

View File

@ -33,14 +33,13 @@ async function sendNotificationList(socket) {
async function sendHeartbeatList(socket, monitorID, toUser = false, overwrite = false) { async function sendHeartbeatList(socket, monitorID, toUser = false, overwrite = false) {
const timeLogger = new TimeLogger(); const timeLogger = new TimeLogger();
let monitor = userMonitorList.getMonitor(socket.userID, monitorID); let list = await R.find("heartbeat", `
monitor_id = ?
if (! monitor) { ORDER BY time DESC
console.error("No this monitor??"); LIMIT 100
return; `, [
} monitorID,
])
let list = monitor.getCachedHeartbeatList().array;
let result = []; let result = [];

View File

@ -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;

View File

@ -6,20 +6,14 @@ dayjs.extend(utc)
dayjs.extend(timezone) dayjs.extend(timezone)
const axios = require("axios"); const axios = require("axios");
const { Prometheus } = require("../prometheus"); 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 { tcping, ping, dnsResolve, checkCertificate, checkStatusCode, getTotalClientInRoom } = require("../util-server");
const { R } = require("redbean-node"); const { R } = require("redbean-node");
const { BeanModel } = require("redbean-node/dist/bean-model"); const { BeanModel } = require("redbean-node/dist/bean-model");
const { Notification } = require("../notification") const { Notification } = require("../notification")
const { userMonitorList } = require("../user-monitor-list"); const { userMonitorList } = require("../user-monitor-list");
const LimitArray = require("../limit-array");
const version = require("../../package.json").version; const version = require("../../package.json").version;
/**
* Master List
*/
const cachedHeartbeatList = {};
/** /**
* status: * status:
* 0 = DOWN * 0 = DOWN
@ -27,29 +21,6 @@ const cachedHeartbeatList = {};
* 2 = PENDING * 2 = PENDING
*/ */
class Monitor extends BeanModel { 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() { async toJSON() {
let notificationIDList = {}; let notificationIDList = {};
@ -112,6 +83,7 @@ class Monitor extends BeanModel {
let prometheus = new Prometheus(this); let prometheus = new Prometheus(this);
const beat = async () => { const beat = async () => {
// Expose here for prometheus update // Expose here for prometheus update
// undefined if not https // undefined if not https
let tlsInfo = undefined; let tlsInfo = undefined;
@ -330,37 +302,22 @@ class Monitor extends BeanModel {
console.warn(`Monitor #${this.id} '${this.name}': Failing: ${bean.msg} | Type: ${this.type}`) 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()); io.to(this.user_id).emit("heartbeat", bean.toJSON());
Monitor.sendStats(io, this.id, this.user_id) Monitor.sendStats(io, this.id, this.user_id)
await R.store(bean); await R.store(bean);
// Also add to cache
this.getCachedHeartbeatList().add(bean);
prometheus.update(bean, tlsInfo); prometheus.update(bean, tlsInfo);
previousBeat = bean; previousBeat = bean;
if (this.beanMeta.extraData.stop !== true) { this.heartbeatInterval = setTimeout(beat, this.interval * 1000);
this.heartbeatInterval = setTimeout(beat, this.interval * 1000);
}
} }
beat(); beat();
} }
stop() { stop() {
console.log(`[Monitor: ${this.id}] Stop`);
debug("Stop " + this.beanMeta.extraData.uniqueNumber)
clearTimeout(this.heartbeatInterval); clearTimeout(this.heartbeatInterval);
this.beanMeta.extraData.stop = true;
} }
/** /**

View File

@ -283,7 +283,8 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString();
bean.import(monitor) bean.import(monitor)
bean.user_id = socket.userID bean.user_id = socket.userID
await R.store(bean); await R.store(bean)
await updateMonitorNotification(bean.id, notificationIDList) await updateMonitorNotification(bean.id, notificationIDList)
await startMonitor(socket.userID, bean.id); 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); let monitor = userMonitorList.getMonitor(socket.userID, monitorID);
if (monitor) { if (monitor) {
monitor.stop();
userMonitorList.delete(socket.userID, monitorID); userMonitorList.delete(socket.userID, monitorID);
} }
@ -676,7 +678,7 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString();
}); });
console.log("Starting All Monitors"); console.log("Starting All Monitors");
await initMonitors(); await startMonitors();
console.log("Init the server"); console.log("Init the server");
@ -816,18 +818,24 @@ async function startMonitor(userID, monitorID) {
let monitor = await R.findOne("monitor", " id = ? ", [ let monitor = await R.findOne("monitor", " id = ? ", [
monitorID, monitorID,
]); ])
let oldMonitor = userMonitorList.getMonitor(userID, monitorID);
if (oldMonitor) {
oldMonitor.stop();
}
userMonitorList.add(userID, monitor); userMonitorList.add(userID, monitor);
monitor.start(io) monitor.start(io)
} }
async function restartMonitor(userID, monitorID) { async function restartMonitor(userID, monitorID) {
return await startMonitor(userID, monitorID); return await startMonitor(userID, monitorID)
} }
async function pauseMonitor(userID, monitorID) { async function pauseMonitor(userID, monitorID) {
await checkOwner(userID, monitorID); await checkOwner(userID, monitorID)
console.log(`Pause Monitor: ${monitorID} User ID: ${userID}`) console.log(`Pause Monitor: ${monitorID} User ID: ${userID}`)
@ -839,7 +847,6 @@ async function pauseMonitor(userID, monitorID) {
let monitor = userMonitorList.getMonitor(userID, monitorID); let monitor = userMonitorList.getMonitor(userID, monitorID);
if (monitor) { if (monitor) {
monitor.active = 0;
monitor.stop(); monitor.stop();
} }
} }
@ -847,21 +854,14 @@ async function pauseMonitor(userID, monitorID) {
/** /**
* Resume active monitors * Resume active monitors
*/ */
async function initMonitors() { async function startMonitors() {
// Init all monitors let list = await R.find("monitor", " active = 1 ");
let list = await R.find("monitor");
let activeList = [];
for (let monitor of list) { for (let monitor of list) {
userMonitorList.add(monitor.user_id, monitor); userMonitorList.add(monitor.user_id, monitor);
if (monitor.active) {
activeList.push(monitor);
}
} }
// Start active monitors only delayStartMonitors(list);
delayStartMonitors(activeList);
} }
/** /**

View File

@ -4,36 +4,14 @@
class UserMonitorList { class UserMonitorList {
list = {}; list = {};
/**
* Add or update
* @param userID
* @param monitor
*/
add(userID, monitor) { add(userID, monitor) {
if (! this.list[userID]) { if (! this.list[userID]) {
this.list[userID] = {}; this.list[userID] = {};
} }
// Stopped the old monitor if same id
this.stop(userID, monitor.id);
this.list[userID][monitor.id] = monitor; 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) { delete(userID, monitorID) {
this.stop(userID, monitorID);
let monitorList = this.getMonitorList(userID); let monitorList = this.getMonitorList(userID);
delete monitorList[monitorID]; delete monitorList[monitorID];
} }