From 07ecc59611beccdf4a8295866b9521e2363deb67 Mon Sep 17 00:00:00 2001 From: BluJ Date: Thu, 27 Apr 2023 11:56:39 -0600 Subject: [PATCH] feat: Add in more options in the adding of a daemon --- lib/health/checkFns/checkPortListening.ts | 45 ++++++++++++++++------- lib/mainFn/Daemons.ts | 5 +-- lib/util/index.ts | 8 ++-- 3 files changed, 37 insertions(+), 21 deletions(-) diff --git a/lib/health/checkFns/checkPortListening.ts b/lib/health/checkFns/checkPortListening.ts index fb74902..fc98d29 100644 --- a/lib/health/checkFns/checkPortListening.ts +++ b/lib/health/checkFns/checkPortListening.ts @@ -19,19 +19,36 @@ export function containsAddress(x: string, port: number) { export async function checkPortListening( effects: Effects, port: number, - { - error = `Port ${port} is not listening`, - message = `Port ${port} is available`, - } = {}, + options: { + errorMessage: string + successMessage: string + timeoutMessage?: string + timeout?: number + }, ): Promise { - const hasAddress = - containsAddress(await effects.runCommand(`cat /proc/net/tcp`), port) || - containsAddress(await effects.runCommand("cat /proc/net/udp"), port) - if (hasAddress) { - return { status: "passing", message } - } - return { - status: "failing", - message: error, - } + return Promise.race([ + Promise.resolve().then(async () => { + const hasAddress = + containsAddress(await effects.runCommand(`cat /proc/net/tcp`), port) || + containsAddress(await effects.runCommand("cat /proc/net/udp"), port) + if (hasAddress) { + return { status: "passing", message: options.successMessage } + } + return { + status: "failing", + message: options.errorMessage, + } + }), + new Promise((resolve) => { + setTimeout( + () => + resolve({ + status: "failing", + message: + options.timeoutMessage || `Timeout trying to check port ${port}`, + }), + options.timeout ?? 1_000, + ) + }), + ]) } diff --git a/lib/mainFn/Daemons.ts b/lib/mainFn/Daemons.ts index 91aedb4..b47f7d3 100644 --- a/lib/mainFn/Daemons.ts +++ b/lib/mainFn/Daemons.ts @@ -13,10 +13,7 @@ type Daemon< command: ValidIfNoStupidEscape | [string, ...string[]] ready: { - display: null | { - name: string - message: string - } + display: string | null fn: () => Promise | CheckResult trigger?: Trigger } diff --git a/lib/util/index.ts b/lib/util/index.ts index 051d85e..75265f0 100644 --- a/lib/util/index.ts +++ b/lib/util/index.ts @@ -73,9 +73,11 @@ export type Utils = { ) => Promise checkPortListening( port: number, - options?: { - error?: string - message?: string + options: { + errorMessage: string + successMessage: string + timeoutMessage?: string + timeout?: number }, ): Promise checkWebUrl(