Allow for performance monitoring

This commit is contained in:
Lukas | AstroGD 2023-09-16 15:19:56 +02:00
parent f0e06891f6
commit 67e3a84168
Signed by: AstroGD
GPG Key ID: 82A5E6C236C535AA
4 changed files with 50 additions and 18 deletions

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{ {
"name": "@astrogd/eu.astrogd.uptime-kuma-push-monitor", "name": "@astrogd/eu.astrogd.uptime-kuma-push-monitor",
"version": "1.0.0-dev.4", "version": "1.0.0-dev.5",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@astrogd/eu.astrogd.uptime-kuma-push-monitor", "name": "@astrogd/eu.astrogd.uptime-kuma-push-monitor",
"version": "1.0.0-dev.4", "version": "1.0.0-dev.5",
"license": "UNLICENSED", "license": "UNLICENSED",
"dependencies": { "dependencies": {
"axios": "^1.5.0" "axios": "^1.5.0"

View File

@ -1,6 +1,6 @@
{ {
"name": "@astrogd/eu.astrogd.uptime-kuma-push-monitor", "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", "description": "Handles uptime kuma push monitor uptime requests",
"main": "dist/index.js", "main": "dist/index.js",
"types": "dist/types/", "types": "dist/types/",

View File

@ -1,4 +1,4 @@
import { sendHeartbeat, sendShutdownNotification } from "./requestHandler"; import { sendHeartbeat, sendDownNotification } from "./requestHandler";
/** /**
* Defines an uptime kuma monitor * Defines an uptime kuma monitor
@ -64,6 +64,12 @@ export class PushMonitor {
*/ */
private _debug: boolean = false; private _debug: boolean = false;
/**
* Performance measurment function
* @private
*/
private _performance: (() => (Promise<number> | number)) | undefined;
/** /**
* Returns the singleton instance of the PushMonitor * Returns the singleton instance of the PushMonitor
*/ */
@ -103,7 +109,7 @@ export class PushMonitor {
url, url,
heartbeatIntervall, heartbeatIntervall,
}, },
lastHeartbeat: new Date(0), lastHeartbeat: new Date(new Date().getTime() + 10000 - heartbeatIntervall * 1000),
}); });
return this; return this;
} }
@ -128,6 +134,16 @@ export class PushMonitor {
return this; return this;
} }
/**
* Sets the performance handler
* @param handler The performance handler
* @returns This instance
*/
public setPerformanceHandler(handler?: () => (Promise<number> | number)): this {
this._performance = handler;
return this;
}
/** /**
* Initializes the timer * Initializes the timer
* @private * @private
@ -137,13 +153,29 @@ export class PushMonitor {
clearInterval(this._checkInterval); clearInterval(this._checkInterval);
} }
this._checkInterval = setInterval(() => { 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) => { this._monitors.forEach((monitor) => {
if (new Date().getTime() - monitor.lastHeartbeat.getTime() > monitor.monitor.heartbeatIntervall * 1000) { if (new Date().getTime() - monitor.lastHeartbeat.getTime() > monitor.monitor.heartbeatIntervall * 1000) {
sendHeartbeat(monitor.monitor.url, this._debug); sendHeartbeat(monitor.monitor.url, performance, this._debug);
monitor.lastHeartbeat = new Date(); monitor.lastHeartbeat = new Date();
} }
}); });
} catch (error) {
console.error(`[eu.astrogd.uptime-kuma-push-monitor] (${new Date().toISOString()}) <internal.interval>: ${error}`);
}
isRunning = false;
}, 1000); }, 1000);
} }
@ -168,7 +200,7 @@ export class PushMonitor {
const promises: Promise<void>[] = []; const promises: Promise<void>[] = [];
this._monitors.forEach((monitor) => { 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); return Promise.all(promises);

View File

@ -6,14 +6,14 @@ import axios from "axios";
* @param debug Whether to log debug information * @param debug Whether to log debug information
* @private * @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); const useUrl = new URL(url);
try { 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) if (debug)
console.log( console.log(
`[eu.astrogd.uptime-kuma-push-monitor] (${new Date().toISOString()}) <${url}>: ${response.status} ${response.statusText} - ${ `[eu.astrogd.uptime-kuma-push-monitor] (${new Date().toISOString()}) <${url}>: ${response.status} ${response.statusText} - ${
response.data response.data.toString()
}` }`
); );
} catch (error) { } catch (error) {
@ -27,14 +27,14 @@ export async function sendHeartbeat(url: string, debug: boolean = false) {
* @param debug Whether to log debug information * @param debug Whether to log debug information
* @private * @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); const useUrl = new URL(url);
try { try {
const response = await axios.get(`${useUrl.origin}/${useUrl.pathname}?status=DOWN&msg=${msg}`); const response = await axios.get(`${useUrl.origin}/${useUrl.pathname}?status=DOWN&msg=${msg}`);
if (debug) if (debug)
console.log( console.log(
`[eu.astrogd.uptime-kuma-push-monitor] (${new Date().toISOString()}) <${url}>: ${response.status} ${response.statusText} - ${ `[eu.astrogd.uptime-kuma-push-monitor] (${new Date().toISOString()}) <${url}>: ${response.status} ${response.statusText} - ${
response.data response.data.toString()
}` }`
); );
} catch (error) { } catch (error) {