[enh] display extra message info about state
This commit is contained in:
@@ -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 {}
|
||||
}
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user