Handle error calls
This commit is contained in:
+38
-1
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user