diff --git a/server/model/monitor.js b/server/model/monitor.js index 4ff0b4cb9..49fcfb303 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -6,7 +6,7 @@ dayjs.extend(utc) dayjs.extend(timezone) const axios = require("axios"); const { Prometheus } = require("../prometheus"); -const { debug, UP, DOWN, PENDING } = require("../../src/util"); +const { debug, UP, DOWN, PENDING, flipStatus } = require("../../src/util"); const { tcping, ping, checkCertificate } = require("../util-server"); const { R } = require("redbean-node"); const { BeanModel } = require("redbean-node/dist/bean-model"); @@ -44,7 +44,7 @@ class Monitor extends BeanModel { interval: this.interval, keyword: this.keyword, ignoreTls: this.getIgnoreTls(), - upsideDown: this.getUpsideDown(), + upsideDown: this.isUpsideDown(), notificationIDList, }; } @@ -61,7 +61,7 @@ class Monitor extends BeanModel { * Parse to boolean * @returns {boolean} */ - getUpsideDown() { + isUpsideDown() { return Boolean(this.upsideDown); } @@ -86,6 +86,10 @@ class Monitor extends BeanModel { bean.time = R.isoDateTime(dayjs.utc()); bean.status = DOWN; + if (this.isUpsideDown()) { + bean.status = flipStatus(bean.status); + } + // Duration if (! isFirstBeat) { bean.duration = dayjs(bean.time).diff(dayjs(previousBeat.time), "second"); @@ -155,14 +159,29 @@ class Monitor extends BeanModel { bean.status = UP; } + if (this.isUpsideDown()) { + bean.status = flipStatus(bean.status); + + if (bean.status === DOWN) { + throw new Error("Flip UP to DOWN"); + } + } + retries = 0; } catch (error) { - if ((this.maxretries > 0) && (retries < this.maxretries)) { + + bean.msg = error.message; + + // If UP come in here, it must be upside down mode + // Just reset the retries + if (this.isUpsideDown() && bean.status === UP) { + retries = 0; + + } else if ((this.maxretries > 0) && (retries < this.maxretries)) { retries++; bean.status = PENDING; } - bean.msg = error.message; } // * ? -> ANY STATUS = important [isFirstBeat] diff --git a/src/util.js b/src/util.js index 68b10c02b..9b4c4dab7 100644 --- a/src/util.js +++ b/src/util.js @@ -1,9 +1,19 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -exports.debug = exports.ucfirst = exports.sleep = exports.PENDING = exports.UP = exports.DOWN = void 0; +exports.debug = exports.ucfirst = exports.sleep = exports.flipStatus = exports.PENDING = exports.UP = exports.DOWN = void 0; exports.DOWN = 0; exports.UP = 1; exports.PENDING = 2; +function flipStatus(s) { + if (s === exports.UP) { + return exports.DOWN; + } + if (s === exports.DOWN) { + return exports.UP; + } + return s; +} +exports.flipStatus = flipStatus; function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } diff --git a/src/util.ts b/src/util.ts index 1ea8db1fe..23446c830 100644 --- a/src/util.ts +++ b/src/util.ts @@ -7,6 +7,18 @@ export const DOWN = 0; export const UP = 1; export const PENDING = 2; +export function flipStatus(s) { + if (s === UP) { + return DOWN; + } + + if (s === DOWN) { + return UP; + } + + return s; +} + export function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); }