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",
"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"

View File

@ -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/",

View File

@ -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> | 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> | 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()}) <internal.interval>: ${error}`);
}
isRunning = false;
}, 1000);
}
@ -168,7 +200,7 @@ export class PushMonitor {
const promises: Promise<void>[] = [];
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);

View File

@ -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) {