From f62a19db01cee5854b91cbd34d38f26be5be10a3 Mon Sep 17 00:00:00 2001 From: Lucy Cifferello <12953208+elvece@users.noreply.github.com> Date: Wed, 23 Nov 2022 11:25:06 -0700 Subject: [PATCH] use modern health check standards --- scripts/deps.ts | 4 ++- scripts/embassy.ts | 2 +- scripts/procedures/health.ts | 52 ++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 scripts/procedures/health.ts diff --git a/scripts/deps.ts b/scripts/deps.ts index c4bee22..6cb9153 100644 --- a/scripts/deps.ts +++ b/scripts/deps.ts @@ -1 +1,3 @@ -export * from "https://deno.land/x/embassyd_sdk@v0.3.1.1.4/mod.ts"; +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/util.ts"; +export * from "https://deno.land/x/embassyd_sdk@v0.3.3.0.4/healthUtil.ts"; \ No newline at end of file diff --git a/scripts/embassy.ts b/scripts/embassy.ts index 8d2f468..df6835b 100644 --- a/scripts/embassy.ts +++ b/scripts/embassy.ts @@ -2,4 +2,4 @@ export { setConfig } from "./procedures/setConfig.ts"; export { getConfig } from "./procedures/getConfig.ts"; export { properties } from "./procedures/properties.ts"; export { migration } from "./procedures/migrations.ts"; -export { health } from "./procedures/healthChecks.ts"; +export { health } from "./procedures/health.ts"; diff --git a/scripts/procedures/health.ts b/scripts/procedures/health.ts new file mode 100644 index 0000000..4810a45 --- /dev/null +++ b/scripts/procedures/health.ts @@ -0,0 +1,52 @@ +import { types as T, checkWebUrl, catchError } from "../deps.ts"; + +export const health: T.ExpectedExports.health = { + // deno-lint-ignore require-await + async "interface"(effects, duration) { + // Checks that the server is running and reachable via http + return healthWeb(effects, duration); + }, + // deno-lint-ignore require-await + async "database"(effects, duration) { + // Checks that the backend is reachable via graphQL + return healthApi(effects, duration); + }, +}; + +// deno-lint-ignore require-await +const healthWeb: T.ExpectedExports.health[""] = async (effects, duration) => { + return checkWebUrl("http://photoview.embassy")(effects, duration).catch(catchError(effects)) +}; + +const healthApi: T.ExpectedExports.health[""] = async (effects, duration) => { + await guardDurationAboveMinimum({ duration, minimumTime: 15000 }); + + return effects.fetch("http://photoview.embassy:80/api/graphql", { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({"operationName":"CheckInitialSetup","variables":{},"query":"query CheckInitialSetup { siteInfo { initialSetup }}"}) + }) + .then((_) => ok) + .catch((e) => { + effects.error(`${e}`) + return error(`The Photoview API is unreachable`) + }); +}; + +// *** HELPER FUNCTIONS *** // + +// Ensure the starting duration is pass a minimum +const guardDurationAboveMinimum = ( + input: { duration: number; minimumTime: number }, +) => + (input.duration <= input.minimumTime) + ? Promise.reject(errorCode(60, "Starting")) + : null; + +const errorCode = (code: number, error: string) => ({ + "error-code": [code, error] as const, +}); +const error = (error: string) => ({ error }); +const ok = { result: null };