Handle error calls

This commit is contained in:
2023-09-16 14:49:52 +02:00
parent 291357998c
commit f0e06891f6
4 changed files with 63 additions and 5 deletions
+38 -1
View File
@@ -1,4 +1,4 @@
import sendHeartbeat from "./requestHandler";
import { sendHeartbeat, sendShutdownNotification } from "./requestHandler";
/**
* Defines an uptime kuma monitor
@@ -118,6 +118,16 @@ export class PushMonitor {
return this;
}
/**
* Enables the sending of shutdown down notifications
* !! This will overwrite the uncaughtException handler function !!
* @returns This instance
*/
public enableShutdownNotifications(): this {
this.initEventHandlers();
return this;
}
/**
* Initializes the timer
* @private
@@ -136,4 +146,31 @@ export class PushMonitor {
});
}, 1000);
}
/**
* Initializes event handlers
* @private
*/
private initEventHandlers() {
process.on("uncaughtException", async (err, origin) => {
console.error(origin + "\n" + err.stack);
await this.sendShutdownNotification(`Uncaught Exception (${err.message})`);
process.exitCode = 1;
process.exit(1);
});
}
/**
* Sends a shutdown notification to all monitors
* @param msg The message to send
*/
private async sendShutdownNotification(msg: string = "Shutdown") {
const promises: Promise<void>[] = [];
this._monitors.forEach((monitor) => {
promises.push(sendShutdownNotification(monitor.monitor.url, msg, this._debug));
});
return Promise.all(promises);
}
}