-
Notifications
You must be signed in to change notification settings - Fork 0
/
healthUtil.ts
48 lines (44 loc) · 1.53 KB
/
healthUtil.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { Effects, ResultType } from "./types.ts";
import { error, errorCode, isKnownError, ok } from "./util.ts";
export const checkWebUrl: (
url: string,
) => (effects: Effects, duration: number) => Promise<ResultType<null | void>> =
(url) => {
return async (effects, duration) => {
let errorValue;
if (
// deno-lint-ignore no-cond-assign
errorValue = guardDurationAboveMinimum({ duration, minimumTime: 5000 })
) return errorValue;
return await effects.fetch(url)
.then((_) => ok)
.catch((e) => {
effects.warn(`Error while fetching URL: ${url}`);
effects.error(JSON.stringify(e));
effects.error(e.toString());
return error(`Error while fetching URL: ${url}`);
});
};
};
export const runHealthScript =
({ command, args }: { command: string; args: string[] }) =>
async (
effects: Effects,
_duration: number,
): Promise<ResultType<null | void>> => {
const res = await effects.runCommand({ command, args });
if ("result" in res) {
return { result: null };
} else {
return res;
}
};
// Ensure the starting duration is pass a minimum
export const guardDurationAboveMinimum = (
input: { duration: number; minimumTime: number },
) => (input.duration <= input.minimumTime) ? errorCode(60, "Starting") : null;
export const catchError = (effects: Effects) => (e: unknown) => {
if (isKnownError(e)) return e;
effects.error(`Health check failed: ${e}`);
return error("Error while running health check");
};