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

View File

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

View File

@ -35,7 +35,7 @@ const executor = async () => {
config.pollEndpoints.map(async endpoint => { config.pollEndpoints.map(async endpoint => {
const endpointState = state.get(endpoint.name) const endpointState = state.get(endpoint.name)
if (endpointState === undefined) console.log(`Could not find endpoint for ${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}`) console.log(`Time to poll ${endpoint.name}`)
endpointState.lastExec = curTime endpointState.lastExec = curTime
if (await isEndpointUp(endpoint)) handleUp(endpointState, curTime, endpoint) if (await isEndpointUp(endpoint)) handleUp(endpointState, curTime, endpoint)