diff --git a/scripts/deps.ts b/scripts/deps.ts index 12d1eb5..ee0acbe 100644 --- a/scripts/deps.ts +++ b/scripts/deps.ts @@ -1 +1,2 @@ export * from "https://deno.land/x/embassyd_sdk@v0.3.3.0.4/mod.ts"; +export * from "https://deno.land/x/embassyd_sdk@v0.3.3.0.4/healthUtil.ts"; diff --git a/scripts/procedures/health.ts b/scripts/procedures/health.ts index 153d850..df0a9b8 100644 --- a/scripts/procedures/health.ts +++ b/scripts/procedures/health.ts @@ -1,51 +1,8 @@ -import { matches, types as T } from "../deps.ts"; -const { string, shape, tuple, number } = matches; -const isError = shape({ error: string }).test; -const isErrorCode = shape({ "error-code": tuple(number, string) }).test; -const error = (error: string) => ({ error }); -const errorCode = (code: number, error: string) => ({ - "error-code": [code, error] as const, -}); -const ok = { result: null }; -/** Transform the error into ResultType, and just return the thrown ResultType */ -const catchError = (effects: T.Effects) => - (e: unknown) => { - if (isError(e)) return e; - if (isErrorCode(e)) return e; - effects.error(`Health check failed: ${e}`); - return errorCode(61, "Health check has never run"); - }; +import { types as T, checkWebUrl, catchError } from "../deps.ts"; -/** Call to make sure the duration is pass a minimum */ -const guardDurationAboveMinimum = ( - input: { duration: number; minimumTime: number }, -) => - (input.duration <= input.minimumTime) - ? Promise.reject(errorCode(60, "Starting")) - : null; - -const checkHealthDuration = 5000; - -export const healthAlive: T.ExpectedExports.health[""] = async ( - effects, - duration, -) => { - await guardDurationAboveMinimum({ - duration, - minimumTime: checkHealthDuration, - }); - const response = await effects.fetch("http://vaultwarden.embassy:80/alive"); - if (response.ok) { - return ok; - } - return error("The Vaultwarden UI is unreachable"); -}; - -/** These are the health checks in the manifest */ export const health: T.ExpectedExports.health = { - /** Checks that the server is running and reachable via cli */ // deno-lint-ignore require-await - async alive(effects, duration) { - return healthAlive(effects, duration).catch(catchError(effects)); + async "alive"(effects, duration) { + return checkWebUrl("http://vaultwarden.embassy:80/alive")(effects, duration).catch(catchError(effects)) }, -}; +}; \ No newline at end of file