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

View File

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

View File

@@ -6,10 +6,10 @@ export function watcherLoop(
onConnectionClosed: () => Promise<void>, onConnectionClosed: () => Promise<void>,
waitingDelay: number, waitingDelay: number,
recallDelay: number, recallDelay: number,
onStart: () => Promise<void>, onStart: () => Promise<string|undefined>,
onDisconnection: (waitingDelay: number, recallDelay: number, error: any) => Promise<void>, onDisconnection: (waitingDelay: number, recallDelay: number, error: any) => Promise<void>,
onRestart: () => Promise<void>, onRestart: () => Promise<void>,
onRestartSuccess: () => Promise<void>, onRestartSuccess: () => Promise<string|undefined>,
): Watcher { ): Watcher {
let watcher: Watcher = new Watcher(name, recallDelay) let watcher: Watcher = new Watcher(name, recallDelay)
@@ -31,11 +31,11 @@ export function watcherLoop(
if (!hasStarted) { if (!hasStarted) {
hasStarted = true hasStarted = true
await onStart() const message = await onStart()
watcher.stateOK() watcher.stateOK(message)
} else { } else {
await onRestartSuccess() const message = await onRestartSuccess()
watcher.stateRecovered() watcher.stateRecovered(message)
} }
// We reset the delay of reconnection // 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 nodeDownRes: () => void
let nodeDownPromise: Promise<void> = new Promise(res => nodeDownRes = res) let nodeDownPromise: Promise<void> = new Promise(res => nodeDownRes = res)
let message: string|undefined = undefined
async function checkResult(data: any) { async function checkResult(data: any) {
const validity = await checkValidity(data) const validity = await checkValidity(data)
if (validity.error) { if (validity.error) {
throw new UrlWatcherError(validity.error) throw new UrlWatcherError(validity.error)
} }
return validity.info
} }
return watcherLoop( return watcherLoop(
@@ -27,14 +29,14 @@ export function urlWatcher(conf: Conf, checkValidity: (data: any) => Promise<Url
let interval: NodeJS.Timer; let interval: NodeJS.Timer;
try { try {
const res = await Axios.get(urlConf.address) const res = await Axios.get(urlConf.address)
await checkResult(res.data) message = await checkResult(res.data)
} catch (e) { } catch (e) {
throw new UrlWatcherError(e.message || e) throw new UrlWatcherError(e.message || e)
} }
interval = setInterval(async () => { interval = setInterval(async () => {
try { try {
const res = await Axios.get(urlConf.address) const res = await Axios.get(urlConf.address)
await checkResult(res.data) message = await checkResult(res.data)
} catch (e) { } catch (e) {
if (interval) { if (interval) {
clearInterval(interval) clearInterval(interval)
@@ -51,7 +53,10 @@ export function urlWatcher(conf: Conf, checkValidity: (data: any) => Promise<Url
conf.waitingDelay, conf.waitingDelay,
conf.recallDelay, conf.recallDelay,
mail.onEstablished(urlConf.address, () => getOkTitle()), async () => {
await mail.onEstablished(urlConf.address, () => getOkTitle())
return message
},
// When a disconnection is detected // When a disconnection is detected
(waitingDelay: number, recallDelay, error?: any) => { (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) 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 { export class UrlWatcherResult {
error?: string error?: string
info?: string
public static ok() { public static ok(okMessage = '') {
if (okMessage) {
return { info: okMessage }
}
return {} return {}
} }

View File

@@ -10,10 +10,11 @@ export function bmaWatcher(conf: Conf) {
return urlWatcher(conf, async (data) => { return urlWatcher(conf, async (data) => {
const block = data as { medianTime: number } const block = data as { medianTime: number }
if (bmaServer.maxLate && moment().unix() - block.medianTime > bmaServer.maxLate) { const secondsLate = moment().unix() - block.medianTime
return UrlWatcherResult.ko('Server is late') 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}`, name: `BMA ${bmaServer.address}`,
address: bmaServer.address + URL_PATH, 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 => { res.send(watchers.map(watcher => {
return { return {
name: watcher.name, name: watcher.name,
state: watcher.state state: watcher.state,
message: watcher.error,
} }
})) }))
}) })

View File

@@ -4,6 +4,7 @@
<li v-for="w in watchers"> <li v-for="w in watchers">
<i class="fas" v-bind:class="watcherIconClass(w)"></i> <i class="fas" v-bind:class="watcherIconClass(w)"></i>
{{ w.name }} <span v-bind:class="watcherClass(w)">{{ w.state }}</span> {{ w.name }} <span v-bind:class="watcherClass(w)">{{ w.state }}</span>
<span v-if="w.message">({{ displayMessage(w.message) }})</span>
</li> </li>
</ul> </ul>
</div> </div>
@@ -41,6 +42,12 @@
return ['text-danger'] return ['text-danger']
} }
return ['text-info'] return ['text-info']
},
displayMessage(msg) {
if (msg.errorMessage) {
return msg.errorMessage.errorMessage || msg.errorMessage
}
return msg
} }
} }
} }