From 67e3a84168deb25437df43e4a1cf2a2a2f14fe8f Mon Sep 17 00:00:00 2001 From: Lukas | AstroGD Date: Sat, 16 Sep 2023 15:19:56 +0200 Subject: [PATCH] Allow for performance monitoring --- package-lock.json | 4 ++-- package.json | 2 +- src/pushMonitor.ts | 52 ++++++++++++++++++++++++++++++++++--------- src/requestHandler.ts | 10 ++++----- 4 files changed, 50 insertions(+), 18 deletions(-) diff --git a/package-lock.json b/package-lock.json index ca117b0..1418b24 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@astrogd/eu.astrogd.uptime-kuma-push-monitor", - "version": "1.0.0-dev.4", + "version": "1.0.0-dev.5", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@astrogd/eu.astrogd.uptime-kuma-push-monitor", - "version": "1.0.0-dev.4", + "version": "1.0.0-dev.5", "license": "UNLICENSED", "dependencies": { "axios": "^1.5.0" diff --git a/package.json b/package.json index 949fd30..cb48f2a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@astrogd/eu.astrogd.uptime-kuma-push-monitor", - "version": "1.0.0-dev.4", + "version": "1.0.0-dev.5", "description": "Handles uptime kuma push monitor uptime requests", "main": "dist/index.js", "types": "dist/types/", diff --git a/src/pushMonitor.ts b/src/pushMonitor.ts index f96a424..5bc4bbb 100644 --- a/src/pushMonitor.ts +++ b/src/pushMonitor.ts @@ -1,4 +1,4 @@ -import { sendHeartbeat, sendShutdownNotification } from "./requestHandler"; +import { sendHeartbeat, sendDownNotification } from "./requestHandler"; /** * Defines an uptime kuma monitor @@ -64,6 +64,12 @@ export class PushMonitor { */ private _debug: boolean = false; + /** + * Performance measurment function + * @private + */ + private _performance: (() => (Promise | number)) | undefined; + /** * Returns the singleton instance of the PushMonitor */ @@ -103,7 +109,7 @@ export class PushMonitor { url, heartbeatIntervall, }, - lastHeartbeat: new Date(0), + lastHeartbeat: new Date(new Date().getTime() + 10000 - heartbeatIntervall * 1000), }); return this; } @@ -128,6 +134,16 @@ export class PushMonitor { return this; } + /** + * Sets the performance handler + * @param handler The performance handler + * @returns This instance + */ + public setPerformanceHandler(handler?: () => (Promise | number)): this { + this._performance = handler; + return this; + } + /** * Initializes the timer * @private @@ -137,13 +153,29 @@ export class PushMonitor { clearInterval(this._checkInterval); } - this._checkInterval = setInterval(() => { - this._monitors.forEach((monitor) => { - if (new Date().getTime() - monitor.lastHeartbeat.getTime() > monitor.monitor.heartbeatIntervall * 1000) { - sendHeartbeat(monitor.monitor.url, this._debug); - monitor.lastHeartbeat = new Date(); - } - }); + let isRunning = false; + + this._checkInterval = setInterval(async () => { + if (isRunning) return; + isRunning = true; + + try { + let performance: number | undefined = undefined; + + if (this._performance) performance = await this._performance(); + + this._monitors.forEach((monitor) => { + if (new Date().getTime() - monitor.lastHeartbeat.getTime() > monitor.monitor.heartbeatIntervall * 1000) { + sendHeartbeat(monitor.monitor.url, performance, this._debug); + monitor.lastHeartbeat = new Date(); + } + }); + } catch (error) { + console.error(`[eu.astrogd.uptime-kuma-push-monitor] (${new Date().toISOString()}) : ${error}`); + } + + + isRunning = false; }, 1000); } @@ -168,7 +200,7 @@ export class PushMonitor { const promises: Promise[] = []; this._monitors.forEach((monitor) => { - promises.push(sendShutdownNotification(monitor.monitor.url, msg, this._debug)); + promises.push(sendDownNotification(monitor.monitor.url, msg, this._debug)); }); return Promise.all(promises); diff --git a/src/requestHandler.ts b/src/requestHandler.ts index a9498e3..303df73 100644 --- a/src/requestHandler.ts +++ b/src/requestHandler.ts @@ -6,14 +6,14 @@ import axios from "axios"; * @param debug Whether to log debug information * @private */ -export async function sendHeartbeat(url: string, debug: boolean = false) { +export async function sendHeartbeat(url: string, performance?: number, debug: boolean = false) { const useUrl = new URL(url); try { - const response = await axios.get(`${useUrl.origin}/${useUrl.pathname}?status=up&msg=OK`); + const response = await axios.get(`${useUrl.origin}/${useUrl.pathname}?status=up&msg=OK${performance ? `&ping=${performance}` : ""}`); if (debug) console.log( `[eu.astrogd.uptime-kuma-push-monitor] (${new Date().toISOString()}) <${url}>: ${response.status} ${response.statusText} - ${ - response.data + response.data.toString() }` ); } catch (error) { @@ -27,14 +27,14 @@ export async function sendHeartbeat(url: string, debug: boolean = false) { * @param debug Whether to log debug information * @private */ -export async function sendShutdownNotification(url: string, msg: string = "Shutdown",debug: boolean = false) { +export async function sendDownNotification(url: string, msg: string = "Error",debug: boolean = false) { const useUrl = new URL(url); try { const response = await axios.get(`${useUrl.origin}/${useUrl.pathname}?status=DOWN&msg=${msg}`); if (debug) console.log( `[eu.astrogd.uptime-kuma-push-monitor] (${new Date().toISOString()}) <${url}>: ${response.status} ${response.statusText} - ${ - response.data + response.data.toString() }` ); } catch (error) {