Allow for performance monitoring

This commit is contained in:
2023-09-16 15:19:56 +02:00
parent f0e06891f6
commit 67e3a84168
4 changed files with 50 additions and 18 deletions
+42 -10
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);