Skip to content

Commit

Permalink
use modern health check standards
Browse files Browse the repository at this point in the history
  • Loading branch information
elvece committed Nov 23, 2022
1 parent 79bc119 commit f62a19d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
4 changes: 3 additions & 1 deletion scripts/deps.ts
Original file line number Diff line number Diff line change
@@ -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";
2 changes: 1 addition & 1 deletion scripts/embassy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
52 changes: 52 additions & 0 deletions scripts/procedures/health.ts
Original file line number Diff line number Diff line change
@@ -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 };

0 comments on commit f62a19d

Please sign in to comment.