Skip to content

Commit

Permalink
update health check to modern standards
Browse files Browse the repository at this point in the history
  • Loading branch information
elvece committed Dec 2, 2022
1 parent 073fb44 commit c5ca011
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 47 deletions.
1 change: 1 addition & 0 deletions scripts/deps.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "https://deno.land/x/[email protected]/mod.ts";
export * from "https://deno.land/x/[email protected]/healthUtil.ts";
51 changes: 4 additions & 47 deletions scripts/procedures/health.ts
Original file line number Diff line number Diff line change
@@ -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))
},
};
};

0 comments on commit c5ca011

Please sign in to comment.