From f0e06891f6c6d9286dbd73d976257d1a78588eb6 Mon Sep 17 00:00:00 2001 From: Lukas | AstroGD Date: Sat, 16 Sep 2023 14:49:52 +0200 Subject: [PATCH] Handle error calls --- package-lock.json | 4 ++-- package.json | 2 +- src/pushMonitor.ts | 39 ++++++++++++++++++++++++++++++++++++++- src/requestHandler.ts | 23 ++++++++++++++++++++++- 4 files changed, 63 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index 8738918..ca117b0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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" diff --git a/package.json b/package.json index af869b9..949fd30 100644 --- a/package.json +++ b/package.json @@ -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/", diff --git a/src/pushMonitor.ts b/src/pushMonitor.ts index 3486846..f96a424 100644 --- a/src/pushMonitor.ts +++ b/src/pushMonitor.ts @@ -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[] = []; + + this._monitors.forEach((monitor) => { + promises.push(sendShutdownNotification(monitor.monitor.url, msg, this._debug)); + }); + + return Promise.all(promises); + } } diff --git a/src/requestHandler.ts b/src/requestHandler.ts index b7cc947..a9498e3 100644 --- a/src/requestHandler.ts +++ b/src/requestHandler.ts @@ -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}`); + } +} \ No newline at end of file