feat: disable childs if parent is disabled

This commit is contained in:
Peace 2023-01-28 13:39:17 +01:00
parent 97bd306a09
commit aba515e172
No known key found for this signature in database
GPG Key ID: 0EF6B46E172B739F
3 changed files with 54 additions and 16 deletions

View File

@ -81,7 +81,8 @@ class Monitor extends BeanModel {
port: this.port, port: this.port,
maxretries: this.maxretries, maxretries: this.maxretries,
weight: this.weight, weight: this.weight,
active: this.active, active: await this.isActive(),
forceInactive: !await Monitor.isParentActive(this.id),
type: this.type, type: this.type,
interval: this.interval, interval: this.interval,
retryInterval: this.retryInterval, retryInterval: this.retryInterval,
@ -141,6 +142,16 @@ class Monitor extends BeanModel {
return data; return data;
} }
/**
* Checks if the monitor is active based on itself and its parents
* @returns {Promise<Boolean>}
*/
async isActive() {
const parentActive = await Monitor.isParentActive(this.id);
return this.active && parentActive;
}
/** /**
* Get all tags applied to this monitor * Get all tags applied to this monitor
* @returns {Promise<LooseObject<any>[]>} * @returns {Promise<LooseObject<any>[]>}
@ -259,22 +270,32 @@ class Monitor extends BeanModel {
} else if (this.type === "group") { } else if (this.type === "group") {
const children = await Monitor.getChildren(this.id); const children = await Monitor.getChildren(this.id);
bean.status = UP; if (children.length > 0) {
bean.msg = "All childs up and running"; bean.status = UP;
for (const child of children) { bean.msg = "All childs up and running";
const lastBeat = await Monitor.getPreviousHeartbeat(child.id); 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 // Only change state if the monitor is in worse conditions then the ones before
if (bean.status === UP && (lastBeat.status === PENDING || lastBeat.status === DOWN)) { if (bean.status === UP && (lastBeat.status === PENDING || lastBeat.status === DOWN)) {
bean.status = lastBeat.status; bean.status = lastBeat.status;
} else if (bean.status === PENDING && lastBeat.status === DOWN) { } else if (bean.status === PENDING && lastBeat.status === DOWN) {
bean.status = lastBeat.status; 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") { } else if (this.type === "http" || this.type === "keyword") {
// Do not do any queries/high loading things before the "bean.ping" // Do not do any queries/high loading things before the "bean.ping"
let startTime = dayjs().valueOf(); let startTime = dayjs().valueOf();
@ -1366,6 +1387,7 @@ class Monitor extends BeanModel {
/** /**
* Gets recursive all child ids * Gets recursive all child ids
* @param {number} monitorID ID of the monitor to get
* @returns {Promise<Array>} * @returns {Promise<Array>}
*/ */
static async getAllChildrenIDs(monitorID) { static async getAllChildrenIDs(monitorID) {
@ -1384,6 +1406,22 @@ class Monitor extends BeanModel {
return childrenIDs; return childrenIDs;
} }
/**
*
* @param {number} monitorID ID of the monitor to get
* @returns {Promise<Boolean>}
*/
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; module.exports = Monitor;

View File

@ -734,7 +734,7 @@ let needSetup = false;
await updateMonitorNotification(bean.id, monitor.notificationIDList); await updateMonitorNotification(bean.id, monitor.notificationIDList);
if (bean.active) { if (bean.isActive()) {
await restartMonitor(socket.userID, bean.id); await restartMonitor(socket.userID, bean.id);
} }
@ -1398,7 +1398,7 @@ let needSetup = false;
await updateMonitorNotification(bean.id, notificationIDList); await updateMonitorNotification(bean.id, notificationIDList);
// If monitor was active start it immediately, otherwise pause it // 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); await startMonitor(socket.userID, bean.id);
} else { } else {
await pauseMonitor(socket.userID, bean.id); await pauseMonitor(socket.userID, bean.id);

View File

@ -25,7 +25,7 @@
<button v-if="monitor.active" class="btn btn-normal" @click="pauseDialog"> <button v-if="monitor.active" class="btn btn-normal" @click="pauseDialog">
<font-awesome-icon icon="pause" /> {{ $t("Pause") }} <font-awesome-icon icon="pause" /> {{ $t("Pause") }}
</button> </button>
<button v-if="! monitor.active" class="btn btn-primary" @click="resumeMonitor"> <button v-if="! monitor.active" class="btn btn-primary" :disabled="monitor.forceInactive" @click="resumeMonitor">
<font-awesome-icon icon="play" /> {{ $t("Resume") }} <font-awesome-icon icon="play" /> {{ $t("Resume") }}
</button> </button>
<router-link :to=" '/edit/' + monitor.id " class="btn btn-normal"> <router-link :to=" '/edit/' + monitor.id " class="btn btn-normal">