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) {
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 = [];

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)
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;
}
/**

View File

@ -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);
}
/**

View File

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