Add configurable defaults for interval and retries

This commit is contained in:
Lumeille 2024-06-02 15:42:38 +10:00
parent d4ade0de35
commit 1fdcdc69c0
3 changed files with 10 additions and 6 deletions

View File

@ -5,6 +5,10 @@ export const defaultInterval = 60
export const defaultNotifyInterval = 300
const configSchema = z.object({
defaults: z.object({
retries: z.number().int().min(0).default(1),
interval: z.number().default(30)
}),
alertEndpoints: z.array(z.object({
enabled: z.boolean(),
name: z.string()
@ -26,10 +30,10 @@ const configSchema = z.object({
]))),
pollEndpoints: z.array(z.object({
name: z.string(),
interval: z.number().default(60),
interval: z.number().optional(),
notifyInterval: z.number().default(300),
alertEndpoints: z.array(z.string()),
retries: z.number().int().min(0).default(1)
retries: z.number().int().min(0).optional()
}).and(z.union([
z.object({
type: z.literal("fetch"),

View File

@ -1,5 +1,5 @@
import { State } from "."
import { Config, defaultNotifyInterval } from "./config"
import { Config, defaultNotifyInterval, config } from "./config"
import { notify } from "./notify"
import { formatTS } from "./utils"
@ -16,7 +16,7 @@ export const handleDown = (endpointState: EndpointState, curTime: number, endpoi
if (curTime - endpointState.lastDownAlert < (endpoint.notifyInterval) * 1000) return
if (endpointState.attemptsFailed > endpoint.retries) {
if (endpointState.attemptsFailed > (endpoint.retries ?? config.defaults.retries)) {
const message = prevDown ?
`[${formatTS(curTime)}] ${endpoint.name} is still down and initially went down at ${formatTS(endpointState.downStart)}` :
`[${formatTS(curTime)}] ${endpoint.name} went down`

View File

@ -35,7 +35,7 @@ const executor = async () => {
config.pollEndpoints.map(async endpoint => {
const endpointState = state.get(endpoint.name)
if (endpointState === undefined) console.log(`Could not find endpoint for ${endpoint.name}`)
else if (curTime - endpointState.lastExec > ((endpoint.interval) * 1000)) {
else if (curTime - endpointState.lastExec > ((endpoint.interval ?? config.defaults.interval) * 1000)) {
console.log(`Time to poll ${endpoint.name}`)
endpointState.lastExec = curTime
if (await isEndpointUp(endpoint)) handleUp(endpointState, curTime, endpoint)