feat: Update with the new side effects, types, and orginization.

This commit is contained in:
BluJ
2023-03-06 12:42:40 -07:00
parent 19504ef559
commit f74be4ec7d
10 changed files with 191 additions and 155 deletions

View File

@@ -3,7 +3,7 @@ import { object, string } from "ts-matches";
export type HealthCheck = (
effects: Types.Effects,
dateMs: number
dateMs: number,
) => Promise<HealthResult>;
export type HealthResult =
| { success: string }
@@ -23,16 +23,23 @@ function safelyStringify(e: unknown) {
}
async function timeoutHealth(
effects: Types.Effects,
timeMs: number
timeMs: number,
): Promise<HealthResult> {
await effects.sleep(timeMs);
return { failure: "Timed out " };
}
/**
* Health runner is usually used during the main function, and will be running in a loop.
* This health check then will be run every intervalS seconds.
* The return needs a create()
* then from there we need a start().
* The stop function is used to stop the health check.
*/
export default function healthRunner(
name: string,
fn: HealthCheck,
{ defaultIntervalS = 60 } = {}
{ defaultIntervalS = 60 } = {},
) {
return {
create(effects: Types.Effects, defaultIntervalCreatedS = defaultIntervalS) {

View File

@@ -1,15 +1,23 @@
import { Effects, ResultType } from "../types";
import { error, errorCode, isKnownError, ok } from "../util";
import { error, errorCode, isKnownError, ok, okOf } from "../util";
import { HealthResult } from "./healthRunner";
/**
* This is a helper function to check if a web url is reachable.
* @param url
* @param createSuccess
* @returns
*/
export const checkWebUrl: (
url: string
url: string,
createSuccess?: null | ((response?: string | null) => string),
) => (
effects: Effects,
duration: number
) => Promise<ResultType<null | void>> = (url) => {
duration: number,
) => Promise<ResultType<HealthResult>> = (url, createSuccess = null) => {
return async (effects, duration) => {
let errorValue;
if (
// deno-lint-ignore no-cond-assign
(errorValue = guardDurationAboveMinimum({ duration, minimumTime: 5000 }))
) {
return errorValue;
@@ -17,7 +25,12 @@ export const checkWebUrl: (
return await effects
.fetch(url)
.then((_) => ok)
.then((x) =>
okOf({
success:
createSuccess?.(x.body) ?? `Successfully fetched URL: ${url}`,
}),
)
.catch((e) => {
effects.warn(`Error while fetching URL: ${url}`);
effects.error(JSON.stringify(e));
@@ -27,17 +40,38 @@ export const checkWebUrl: (
};
};
/**
* Running a health script, is used when we want to have a simple
* script in bash or something like that. It should return something that is useful
* in {result: string} else it is considered an error
* @param param0
* @returns
*/
export const runHealthScript =
({ command, args }: { command: string; args: string[] }) =>
({
command,
args,
message,
}: {
command: string;
args: string[];
message: ((result: unknown) => string) | null;
}) =>
async (
effects: Effects,
_duration: number
): Promise<ResultType<null | void>> => {
_duration: number,
): Promise<ResultType<HealthResult>> => {
const res = await effects.runCommand({ command, args });
if ("result" in res) {
return { result: null };
return {
result: {
success:
message?.(res) ??
`Have ran script ${command} and the result: ${res.result}`,
},
};
} else {
return res;
throw res;
}
};