From aba515e17245a743362380d118a12a73c6113f08 Mon Sep 17 00:00:00 2001 From: Peace Date: Sat, 28 Jan 2023 13:39:17 +0100 Subject: [PATCH] feat: disable childs if parent is disabled --- server/model/monitor.js | 64 ++++++++++++++++++++++++++++++++--------- server/server.js | 4 +-- src/pages/Details.vue | 2 +- 3 files changed, 54 insertions(+), 16 deletions(-) diff --git a/server/model/monitor.js b/server/model/monitor.js index 821a8dbb4..e81c4ad38 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -81,7 +81,8 @@ class Monitor extends BeanModel { port: this.port, maxretries: this.maxretries, weight: this.weight, - active: this.active, + active: await this.isActive(), + forceInactive: !await Monitor.isParentActive(this.id), type: this.type, interval: this.interval, retryInterval: this.retryInterval, @@ -141,6 +142,16 @@ class Monitor extends BeanModel { return data; } + /** + * Checks if the monitor is active based on itself and its parents + * @returns {Promise} + */ + async isActive() { + const parentActive = await Monitor.isParentActive(this.id); + + return this.active && parentActive; + } + /** * Get all tags applied to this monitor * @returns {Promise[]>} @@ -259,22 +270,32 @@ class Monitor extends BeanModel { } else if (this.type === "group") { const children = await Monitor.getChildren(this.id); - bean.status = UP; - bean.msg = "All childs up and running"; - for (const child of children) { - const lastBeat = await Monitor.getPreviousHeartbeat(child.id); + if (children.length > 0) { + bean.status = UP; + bean.msg = "All childs up and running"; + for (const child of children) { + if (!child.active) { + // Ignore inactive childs + continue; + } + const lastBeat = await Monitor.getPreviousHeartbeat(child.id); - // Only change state if the monitor is in worse conditions then the ones before - if (bean.status === UP && (lastBeat.status === PENDING || lastBeat.status === DOWN)) { - bean.status = lastBeat.status; - } else if (bean.status === PENDING && lastBeat.status === DOWN) { - bean.status = lastBeat.status; + // Only change state if the monitor is in worse conditions then the ones before + if (bean.status === UP && (lastBeat.status === PENDING || lastBeat.status === DOWN)) { + bean.status = lastBeat.status; + } else if (bean.status === PENDING && lastBeat.status === DOWN) { + bean.status = lastBeat.status; + } } + + if (bean.status !== UP) { + bean.msg = "Child inaccessible"; + } + } else { + // Set status pending if group is empty + bean.status = PENDING; } - if (bean.status !== UP) { - bean.msg = "Child inaccessible"; - } } else if (this.type === "http" || this.type === "keyword") { // Do not do any queries/high loading things before the "bean.ping" let startTime = dayjs().valueOf(); @@ -1366,6 +1387,7 @@ class Monitor extends BeanModel { /** * Gets recursive all child ids + * @param {number} monitorID ID of the monitor to get * @returns {Promise} */ static async getAllChildrenIDs(monitorID) { @@ -1384,6 +1406,22 @@ class Monitor extends BeanModel { return childrenIDs; } + + /** + * + * @param {number} monitorID ID of the monitor to get + * @returns {Promise} + */ + static async isParentActive(monitorID) { + const parent = await Monitor.getParent(monitorID); + + if (parent === null) { + return true; + } + + const parentActive = await Monitor.isParentActive(parent.id); + return parent.active && parentActive; + } } module.exports = Monitor; diff --git a/server/server.js b/server/server.js index 003d94868..deca267da 100644 --- a/server/server.js +++ b/server/server.js @@ -734,7 +734,7 @@ let needSetup = false; await updateMonitorNotification(bean.id, monitor.notificationIDList); - if (bean.active) { + if (bean.isActive()) { await restartMonitor(socket.userID, bean.id); } @@ -1398,7 +1398,7 @@ let needSetup = false; await updateMonitorNotification(bean.id, notificationIDList); // If monitor was active start it immediately, otherwise pause it - if (monitorListData[i].active === 1) { + if (monitorListData[i].isActive === 1) { await startMonitor(socket.userID, bean.id); } else { await pauseMonitor(socket.userID, bean.id); diff --git a/src/pages/Details.vue b/src/pages/Details.vue index b4aae8fc9..f2cd1a023 100644 --- a/src/pages/Details.vue +++ b/src/pages/Details.vue @@ -25,7 +25,7 @@ -