From c06eab06470ed4cb9a4c6bff061aede2100c7d31 Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Thu, 18 Jan 2024 23:11:31 +0100 Subject: [PATCH] extracted the redis monitor ot a different monitoring type --- server/model/monitor.js | 9 +------ server/monitor-types/redis.js | 45 +++++++++++++++++++++++++++++++++++ server/uptime-kuma-server.js | 2 ++ server/util-server.js | 35 --------------------------- 4 files changed, 48 insertions(+), 43 deletions(-) create mode 100644 server/monitor-types/redis.js diff --git a/server/model/monitor.js b/server/model/monitor.js index b2fed86f..a4363da1 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -5,7 +5,7 @@ const { log, UP, DOWN, PENDING, MAINTENANCE, flipStatus, MAX_INTERVAL_SECOND, MI SQL_DATETIME_FORMAT } = require("../../src/util"); const { tcping, ping, checkCertificate, checkStatusCode, getTotalClientInRoom, setting, mssqlQuery, postgresQuery, mysqlQuery, setSetting, httpNtlm, radius, grpcQuery, - redisPingAsync, mongodbPing, kafkaProducerAsync, getOidcTokenClientCredentials, rootCertificatesFingerprints, axiosAbortSignal + mongodbPing, kafkaProducerAsync, getOidcTokenClientCredentials, rootCertificatesFingerprints, axiosAbortSignal } = require("../util-server"); const { R } = require("redbean-node"); const { BeanModel } = require("redbean-node/dist/bean-model"); @@ -850,13 +850,6 @@ class Monitor extends BeanModel { bean.msg = resp.code; bean.status = UP; bean.ping = dayjs().valueOf() - startTime; - } else if (this.type === "redis") { - let startTime = dayjs().valueOf(); - - bean.msg = await redisPingAsync(this.databaseConnectionString); - bean.status = UP; - bean.ping = dayjs().valueOf() - startTime; - } else if (this.type in UptimeKumaServer.monitorTypeList) { let startTime = dayjs().valueOf(); const monitorType = UptimeKumaServer.monitorTypeList[this.type]; diff --git a/server/monitor-types/redis.js b/server/monitor-types/redis.js new file mode 100644 index 00000000..29f504f9 --- /dev/null +++ b/server/monitor-types/redis.js @@ -0,0 +1,45 @@ +const { MonitorType } = require("./monitor-type"); +const { UP } = require("../../src/util"); +const redis = require("redis"); + +class RedisMonitorType extends MonitorType { + name = "redis"; + + /** + * @inheritdoc + */ + async check(monitor, heartbeat, _server) { + heartbeat.msg = await this.redisPingAsync(monitor.databaseConnectionString); + heartbeat.status = UP; + } + + /** + * Redis server ping + * @param {string} dsn The redis connection string + * @returns {Promise} Response from redis server + */ + async redisPingAsync(dsn) { + const client = redis.createClient({ + url: dsn, + }); + client.on("error", (err) => { + if (client.isOpen) { + client.disconnect(); + } + throw err; + }); + await client.connect(); + if (!client.isOpen) { + throw new Error("connection isn't open after trying to connect"); + } + const pingResult = client.ping(); + if (client.isOpen) { + client.disconnect(); + } + return pingResult; + } +} + +module.exports = { + RedisMonitorType, +}; diff --git a/server/uptime-kuma-server.js b/server/uptime-kuma-server.js index 2eec9c7c..820f22bb 100644 --- a/server/uptime-kuma-server.js +++ b/server/uptime-kuma-server.js @@ -113,6 +113,7 @@ class UptimeKumaServer { UptimeKumaServer.monitorTypeList["tailscale-ping"] = new TailscalePing(); UptimeKumaServer.monitorTypeList["dns"] = new DnsMonitorType(); UptimeKumaServer.monitorTypeList["mqtt"] = new MqttMonitorType(); + UptimeKumaServer.monitorTypeList["redis"] = new RedisMonitorType(); // Allow all CORS origins (polling) in development let cors = undefined; @@ -516,3 +517,4 @@ const { RealBrowserMonitorType } = require("./monitor-types/real-browser-monitor const { TailscalePing } = require("./monitor-types/tailscale-ping"); const { DnsMonitorType } = require("./monitor-types/dns"); const { MqttMonitorType } = require("./monitor-types/mqtt"); +const { RedisMonitorType } = require("./monitor-types/redis"); diff --git a/server/util-server.js b/server/util-server.js index 8add5bc5..54f26a67 100644 --- a/server/util-server.js +++ b/server/util-server.js @@ -17,7 +17,6 @@ const { Settings } = require("./settings"); const grpc = require("@grpc/grpc-js"); const protojs = require("protobufjs"); const radiusClient = require("node-radius-client"); -const redis = require("redis"); const oidc = require("openid-client"); const tls = require("tls"); @@ -502,40 +501,6 @@ exports.radius = function ( }); }; -/** - * Redis server ping - * @param {string} dsn The redis connection string - * @returns {Promise} Response from redis server - */ -exports.redisPingAsync = function (dsn) { - return new Promise((resolve, reject) => { - const client = redis.createClient({ - url: dsn - }); - client.on("error", (err) => { - if (client.isOpen) { - client.disconnect(); - } - reject(err); - }); - client.connect().then(() => { - if (!client.isOpen) { - client.emit("error", new Error("connection isn't open")); - } - client.ping().then((res, err) => { - if (client.isOpen) { - client.disconnect(); - } - if (err) { - reject(err); - } else { - resolve(res); - } - }).catch(error => reject(error)); - }); - }); -}; - /** * Retrieve value of setting based on key * @param {string} key Key of setting to retrieve