[enh] add url watcher + add optional maxLate parameter for BMA watcher
This commit is contained in:
54
src/lib/watchers/abstract/url-watcher.ts
Normal file
54
src/lib/watchers/abstract/url-watcher.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import {watcherLoop} from "../../watcherLoop";
|
||||
import {Conf, ConfURL} from "../../types/conf";
|
||||
import Axios from "axios";
|
||||
import {mail} from "../../mail";
|
||||
|
||||
export function urlWatcher(conf: Conf, checkValidity: (data: any) => Promise<void>) {
|
||||
|
||||
return async (urlConf: ConfURL) => {
|
||||
|
||||
let nodeDownRes: () => void
|
||||
let nodeDownPromise: Promise<void> = new Promise(res => nodeDownRes = res)
|
||||
|
||||
await watcherLoop(
|
||||
async () => {
|
||||
let interval: NodeJS.Timer;
|
||||
const res = await Axios.get(urlConf.address)
|
||||
await checkValidity(res.data)
|
||||
interval = setInterval(async () => {
|
||||
try {
|
||||
const res = await Axios.get(urlConf.address)
|
||||
await checkValidity(res.data)
|
||||
} catch (e) {
|
||||
if (interval) {
|
||||
clearInterval(interval)
|
||||
}
|
||||
nodeDownRes()
|
||||
// Re-create down promise for future connection trial
|
||||
nodeDownPromise = new Promise(res => nodeDownRes = res)
|
||||
}
|
||||
}, urlConf.frequency)
|
||||
},
|
||||
|
||||
() => nodeDownPromise,
|
||||
|
||||
conf.reconnectionDelays,
|
||||
|
||||
mail.onEstablished(conf, urlConf.address),
|
||||
|
||||
// When a disconnection is detected
|
||||
mail.onDisconnect(conf, urlConf.address),
|
||||
|
||||
async () => {
|
||||
console.log('Trying to connect to %s', urlConf.address)
|
||||
},
|
||||
|
||||
mail.onRestartSuccess(conf, urlConf.address),
|
||||
|
||||
async (e) => {
|
||||
console.error(e.message || e)
|
||||
}
|
||||
)
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
import {watcherLoop} from "../../watcherLoop";
|
||||
import {Conf, ConfBMA} from "../../types/conf";
|
||||
import Axios from "axios";
|
||||
import {mail} from "../../mail";
|
||||
import {urlWatcher} from '../abstract/url-watcher'
|
||||
import {moment} from 'duniter/app/lib/common-libs/moment'
|
||||
|
||||
export function bmaWatcher(conf: Conf) {
|
||||
|
||||
@@ -9,43 +8,14 @@ export function bmaWatcher(conf: Conf) {
|
||||
|
||||
return async (bmaServer: ConfBMA) => {
|
||||
|
||||
let nodeDownRes: () => void
|
||||
let nodeDownPromise: Promise<void> = new Promise(res => nodeDownRes = res)
|
||||
|
||||
await watcherLoop(
|
||||
async () => {
|
||||
await Axios.get(bmaServer.address + URL_PATH)
|
||||
let interval = setInterval(async () => {
|
||||
try {
|
||||
await Axios.get(bmaServer.address + URL_PATH)
|
||||
} catch (e) {
|
||||
clearInterval(interval)
|
||||
nodeDownRes()
|
||||
// Re-create down promise for future connection trial
|
||||
nodeDownPromise = new Promise(res => nodeDownRes = res)
|
||||
}
|
||||
}, bmaServer.frequency)
|
||||
},
|
||||
|
||||
() => nodeDownPromise,
|
||||
|
||||
conf.reconnectionDelays,
|
||||
|
||||
mail.onEstablished(conf, bmaServer.address),
|
||||
|
||||
// When a disconnection is detected
|
||||
mail.onDisconnect(conf, bmaServer.address),
|
||||
|
||||
async () => {
|
||||
console.log('Trying to connect to %s', bmaServer.address)
|
||||
},
|
||||
|
||||
mail.onRestartSuccess(conf, bmaServer.address),
|
||||
|
||||
async (e) => {
|
||||
console.error(e.message)
|
||||
await urlWatcher(conf, async (data) => {
|
||||
const block = data as { medianTime: number }
|
||||
if (bmaServer.maxLate && moment().unix() - block.medianTime > bmaServer.maxLate) {
|
||||
throw 'Server is late'
|
||||
}
|
||||
)
|
||||
|
||||
})({
|
||||
address: bmaServer.address + URL_PATH,
|
||||
frequency: bmaServer.frequency
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
17
src/lib/watchers/dprobe/dprobe-heartbeat-watcher.ts
Normal file
17
src/lib/watchers/dprobe/dprobe-heartbeat-watcher.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import {Conf, ConfDprobeHeartbeat} from "../../types/conf";
|
||||
import {urlWatcher} from '../abstract/url-watcher'
|
||||
import {moment} from 'duniter/app/lib/common-libs/moment'
|
||||
|
||||
export function dprobeHeartbeat(conf: Conf) {
|
||||
|
||||
return async (dconf: ConfDprobeHeartbeat) => {
|
||||
|
||||
await urlWatcher(conf, async (data) => {
|
||||
const last = moment(data, 'YYYY-MM-DD HH:mm:ss\n')
|
||||
const past = moment().diff(last)
|
||||
if (past > dconf.lastBeat) {
|
||||
throw 'Delay is over'
|
||||
}
|
||||
})(dconf)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user