Handle error calls

This commit is contained in:
Lukas | AstroGD 2023-09-16 14:49:52 +02:00
parent 291357998c
commit f0e06891f6
Signed by: AstroGD
GPG Key ID: 82A5E6C236C535AA
4 changed files with 63 additions and 5 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.3",
"version": "1.0.0-dev.4",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@astrogd/eu.astrogd.uptime-kuma-push-monitor",
"version": "1.0.0-dev.3",
"version": "1.0.0-dev.4",
"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.3",
"version": "1.0.0-dev.4",
"description": "Handles uptime kuma push monitor uptime requests",
"main": "dist/index.js",
"types": "dist/types/",

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);
}
}

View File

@ -6,7 +6,7 @@ import axios from "axios";
* @param debug Whether to log debug information
* @private
*/
export default async function sendHeartbeat(url: string, debug: boolean = false) {
export async function sendHeartbeat(url: string, debug: boolean = false) {
const useUrl = new URL(url);
try {
const response = await axios.get(`${useUrl.origin}/${useUrl.pathname}?status=up&msg=OK`);
@ -20,3 +20,24 @@ export default async function sendHeartbeat(url: string, debug: boolean = false)
console.error(`[eu.astrogd.uptime-kuma-push-monitor] (${new Date().toISOString()}) <${url}>: ${error}`);
}
}
/**
* Sends a shutdown notification to the provided URL
* @param url The URL to send the notification to
* @param debug Whether to log debug information
* @private
*/
export async function sendShutdownNotification(url: string, msg: string = "Shutdown",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
}`
);
} catch (error) {
console.error(`[eu.astrogd.uptime-kuma-push-monitor] (${new Date().toISOString()}) <${url}>: ${error}`);
}
}