-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update health check to modern standards
- Loading branch information
Showing
2 changed files
with
5 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}, | ||
}; | ||
}; |