add healthUtil

This commit is contained in:
Chris Guida
2022-10-03 14:55:28 -05:00
parent a30099157a
commit cf98a62c73
3 changed files with 35 additions and 3 deletions

View File

@@ -1,6 +1,5 @@
TEST_FILES := $(shell find ./**/*.ts)
test: $(TEST_FILES)
deno test test.ts
deno check mod.ts
deno check mod.ts

33
healthUtil.ts Normal file
View File

@@ -0,0 +1,33 @@
import { Effects, ExpectedExports, ResultType } from "./types.ts";
export const checkWebUrl: (
url: string,
) => (effects: Effects, duration: number) => Promise<ResultType<null | void>> =
(url) => {
return async (effects, duration) => {
await guardDurationAboveMinimum({ duration, minimumTime: 5000 });
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}`);
});
};
};
// 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 };

2
mod.ts
View File

@@ -1,6 +1,6 @@
export { matches, YAML } from "./dependencies.ts";
export * as types from "./types.ts";
export * as compat from "./compat/mod.ts";
export * as migrations from "./migrations.ts";
export * as healthUtil from "./healthUtil.ts";
export * as util from "./util.ts";