[enh] display extra message info about state

This commit is contained in:
2021-08-13 11:37:31 +02:00
parent 1eaf10b83e
commit 34b624bc5c
7 changed files with 42 additions and 17 deletions

View File

@@ -119,6 +119,7 @@ export const mail = {
`) => {
return async (cc?: string) => {
await queueEmail(`[dw] [${os.hostname}] ${message()}`, getHtml(), cc)
return undefined
}
},
@@ -150,6 +151,7 @@ export const mail = {
return async (cc?: string) => {
console.log(`${message()}`)
await queueEmail(`[dw] [${os.hostname}] ${message()}`, getHtml(), cc)
return undefined
}
},
}

View File

@@ -11,12 +11,14 @@ export class Watcher {
this.changeToState("INIT")
}
public stateOK() {
public stateOK(message = '') {
this.changeToState("OK")
this._error = message
}
public stateRecovered() {
public stateRecovered(message = '') {
this.changeToState("RECOVERED")
this._error = message
}
public stateFailure(error: any) {

View File

@@ -6,10 +6,10 @@ export function watcherLoop(
onConnectionClosed: () => Promise<void>,
waitingDelay: number,
recallDelay: number,
onStart: () => Promise<void>,
onStart: () => Promise<string|undefined>,
onDisconnection: (waitingDelay: number, recallDelay: number, error: any) => Promise<void>,
onRestart: () => Promise<void>,
onRestartSuccess: () => Promise<void>,
onRestartSuccess: () => Promise<string|undefined>,
): Watcher {
let watcher: Watcher = new Watcher(name, recallDelay)
@@ -31,11 +31,11 @@ export function watcherLoop(
if (!hasStarted) {
hasStarted = true
await onStart()
watcher.stateOK()
const message = await onStart()
watcher.stateOK(message)
} else {
await onRestartSuccess()
watcher.stateRecovered()
const message = await onRestartSuccess()
watcher.stateRecovered(message)
}
// We reset the delay of reconnection

View File

@@ -13,12 +13,14 @@ export function urlWatcher(conf: Conf, checkValidity: (data: any) => Promise<Url
let nodeDownRes: () => void
let nodeDownPromise: Promise<void> = new Promise(res => nodeDownRes = res)
let message: string|undefined = undefined
async function checkResult(data: any) {
const validity = await checkValidity(data)
if (validity.error) {
throw new UrlWatcherError(validity.error)
}
return validity.info
}
return watcherLoop(
@@ -27,14 +29,14 @@ export function urlWatcher(conf: Conf, checkValidity: (data: any) => Promise<Url
let interval: NodeJS.Timer;
try {
const res = await Axios.get(urlConf.address)
await checkResult(res.data)
message = await checkResult(res.data)
} catch (e) {
throw new UrlWatcherError(e.message || e)
}
interval = setInterval(async () => {
try {
const res = await Axios.get(urlConf.address)
await checkResult(res.data)
message = await checkResult(res.data)
} catch (e) {
if (interval) {
clearInterval(interval)
@@ -51,7 +53,10 @@ export function urlWatcher(conf: Conf, checkValidity: (data: any) => Promise<Url
conf.waitingDelay,
conf.recallDelay,
mail.onEstablished(urlConf.address, () => getOkTitle()),
async () => {
await mail.onEstablished(urlConf.address, () => getOkTitle())
return message
},
// When a disconnection is detected
(waitingDelay: number, recallDelay, error?: any) => {
@@ -68,7 +73,10 @@ export function urlWatcher(conf: Conf, checkValidity: (data: any) => Promise<Url
console.log('Trying to connect to %s', urlConf.address)
},
mail.onRestartSuccess(urlConf.address, () => getRecoveredTitle()),
async () => {
await mail.onRestartSuccess(urlConf.address, () => getRecoveredTitle())
return message
},
)
}
@@ -83,8 +91,12 @@ export class UrlWatcherError {
export class UrlWatcherResult {
error?: string
info?: string
public static ok() {
public static ok(okMessage = '') {
if (okMessage) {
return { info: okMessage }
}
return {}
}

View File

@@ -10,10 +10,11 @@ export function bmaWatcher(conf: Conf) {
return urlWatcher(conf, async (data) => {
const block = data as { medianTime: number }
if (bmaServer.maxLate && moment().unix() - block.medianTime > bmaServer.maxLate) {
return UrlWatcherResult.ko('Server is late')
const secondsLate = moment().unix() - block.medianTime
if (bmaServer.maxLate && secondsLate > bmaServer.maxLate) {
return UrlWatcherResult.ko('Server is late by ' + (secondsLate / 3600).toFixed(2) + ' hours')
}
return UrlWatcherResult.ok()
return UrlWatcherResult.ok('Server is late by ' + (secondsLate / 3600).toFixed(2) + ' hours')
})({
name: `BMA ${bmaServer.address}`,
address: bmaServer.address + URL_PATH,

View File

@@ -12,7 +12,8 @@ export function webappServe(watchers: Watcher[], host = 'localhost', port = 1050
res.send(watchers.map(watcher => {
return {
name: watcher.name,
state: watcher.state
state: watcher.state,
message: watcher.error,
}
}))
})