Implement feature
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
import sendHeartbeat from "./requestHandler";
|
||||
|
||||
/**
|
||||
* Defines an uptime kuma monitor
|
||||
*/
|
||||
interface Monitor {
|
||||
/**
|
||||
* The url to push heartbeats to
|
||||
*/
|
||||
url: string;
|
||||
/**
|
||||
* The interval in which to push heartbeats in seconds
|
||||
*/
|
||||
heartbeatIntervall: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines a monitor store
|
||||
*/
|
||||
interface MonitorStore {
|
||||
/**
|
||||
* The monitor
|
||||
*/
|
||||
monitor: Monitor;
|
||||
/**
|
||||
* The last heartbeat
|
||||
*/
|
||||
lastHeartbeat: Date;
|
||||
}
|
||||
|
||||
/**
|
||||
* The handler managing all monitors
|
||||
*/
|
||||
export class PushMonitor {
|
||||
/**
|
||||
* The private constructor
|
||||
* @private
|
||||
*/
|
||||
private constructor() {
|
||||
this.initTimer();
|
||||
}
|
||||
|
||||
/**
|
||||
* The singleton instance of the PushMonitor
|
||||
* @private
|
||||
*/
|
||||
private static _instance = new PushMonitor();
|
||||
|
||||
/**
|
||||
* The monitors to manage
|
||||
* @private
|
||||
*/
|
||||
private _monitors: MonitorStore[] = [];
|
||||
|
||||
/**
|
||||
* The check interval
|
||||
* @private
|
||||
*/
|
||||
private _checkInterval: NodeJS.Timer | undefined;
|
||||
|
||||
/**
|
||||
* Whether to log debug information
|
||||
* @private
|
||||
*/
|
||||
private _debug: boolean = false;
|
||||
|
||||
/**
|
||||
* Returns the singleton instance of the PushMonitor
|
||||
*/
|
||||
public static get instance(): PushMonitor {
|
||||
return PushMonitor._instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the monitor
|
||||
* @returns This instance
|
||||
*/
|
||||
public start(): this {
|
||||
this.initTimer();
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops the monitor
|
||||
* @returns This instance
|
||||
*/
|
||||
public stop(): this {
|
||||
if (this._checkInterval) {
|
||||
clearInterval(this._checkInterval);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a new monitor to be managed
|
||||
* @param url {string} The url to push heartbeats to
|
||||
* @param heartbeatIntervall {number} The interval in which to push heartbeats in seconds. Default is 58 seconds
|
||||
* @returns This instance
|
||||
*/
|
||||
public register(url: string, heartbeatIntervall: number = 58): this {
|
||||
this._monitors.push({
|
||||
monitor: {
|
||||
url,
|
||||
heartbeatIntervall,
|
||||
},
|
||||
lastHeartbeat: new Date(0),
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets whether to log debug information
|
||||
* @param debug Whether to log debug information
|
||||
* @returns This instance
|
||||
*/
|
||||
public setDebug(debug: boolean): this {
|
||||
this._debug = debug;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the timer
|
||||
* @private
|
||||
*/
|
||||
private initTimer() {
|
||||
if (this._checkInterval) {
|
||||
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();
|
||||
}
|
||||
});
|
||||
}, 1000);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user