implement upside down mode and ignore tls error

This commit is contained in:
LouisLam 2021-07-31 00:01:04 +08:00
parent 06377af7e5
commit 63f0a36811
3 changed files with 47 additions and 6 deletions

View File

@ -6,7 +6,7 @@ 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 } = require("../../src/util"); const { debug, UP, DOWN, PENDING, flipStatus } = require("../../src/util");
const { tcping, ping, checkCertificate } = require("../util-server"); const { tcping, ping, checkCertificate } = 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");
@ -44,7 +44,7 @@ class Monitor extends BeanModel {
interval: this.interval, interval: this.interval,
keyword: this.keyword, keyword: this.keyword,
ignoreTls: this.getIgnoreTls(), ignoreTls: this.getIgnoreTls(),
upsideDown: this.getUpsideDown(), upsideDown: this.isUpsideDown(),
notificationIDList, notificationIDList,
}; };
} }
@ -61,7 +61,7 @@ class Monitor extends BeanModel {
* Parse to boolean * Parse to boolean
* @returns {boolean} * @returns {boolean}
*/ */
getUpsideDown() { isUpsideDown() {
return Boolean(this.upsideDown); return Boolean(this.upsideDown);
} }
@ -86,6 +86,10 @@ class Monitor extends BeanModel {
bean.time = R.isoDateTime(dayjs.utc()); bean.time = R.isoDateTime(dayjs.utc());
bean.status = DOWN; bean.status = DOWN;
if (this.isUpsideDown()) {
bean.status = flipStatus(bean.status);
}
// Duration // Duration
if (! isFirstBeat) { if (! isFirstBeat) {
bean.duration = dayjs(bean.time).diff(dayjs(previousBeat.time), "second"); bean.duration = dayjs(bean.time).diff(dayjs(previousBeat.time), "second");
@ -155,14 +159,29 @@ class Monitor extends BeanModel {
bean.status = UP; bean.status = UP;
} }
if (this.isUpsideDown()) {
bean.status = flipStatus(bean.status);
if (bean.status === DOWN) {
throw new Error("Flip UP to DOWN");
}
}
retries = 0; retries = 0;
} catch (error) { } 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++; retries++;
bean.status = PENDING; bean.status = PENDING;
} }
bean.msg = error.message;
} }
// * ? -> ANY STATUS = important [isFirstBeat] // * ? -> ANY STATUS = important [isFirstBeat]

View File

@ -1,9 +1,19 @@
"use strict"; "use strict";
Object.defineProperty(exports, "__esModule", { value: true }); 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.DOWN = 0;
exports.UP = 1; exports.UP = 1;
exports.PENDING = 2; 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) { function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms)); return new Promise(resolve => setTimeout(resolve, ms));
} }

View File

@ -7,6 +7,18 @@ export const DOWN = 0;
export const UP = 1; export const UP = 1;
export const PENDING = 2; export const PENDING = 2;
export function flipStatus(s) {
if (s === UP) {
return DOWN;
}
if (s === DOWN) {
return UP;
}
return s;
}
export function sleep(ms) { export function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms)); return new Promise(resolve => setTimeout(resolve, ms));
} }