[state]: watchers return a state
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import * as minimist from 'minimist'
|
import * as minimist from 'minimist'
|
||||||
import * as path from 'path'
|
import * as path from 'path'
|
||||||
import {dwatch} from './lib/dwatch'
|
import {dwatch} from './lib/dwatch'
|
||||||
|
import {Watcher} from "./lib/types/state";
|
||||||
|
|
||||||
process.on('uncaughtException', (err) => {
|
process.on('uncaughtException', (err) => {
|
||||||
// Dunno why this specific exception is not caught
|
// Dunno why this specific exception is not caught
|
||||||
@@ -20,9 +21,9 @@ process.on('unhandledRejection', (err) => {
|
|||||||
console.log('Starting...')
|
console.log('Starting...')
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await dwatch(argv.conf || path.join(__dirname, '../app.yml'))
|
const watchers: Watcher[] = await dwatch(argv.conf || path.join(__dirname, '../app.yml'))
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
// webappServe(watchers)
|
||||||
console.error(e)
|
console.error(e)
|
||||||
}
|
}
|
||||||
})()
|
})()
|
||||||
|
|
||||||
|
|||||||
@@ -5,16 +5,19 @@ import {ws2pWatcher} from "./watchers/ws2p/ws2p-watcher";
|
|||||||
import {bmaWatcher} from "./watchers/bma/bma-watcher";
|
import {bmaWatcher} from "./watchers/bma/bma-watcher";
|
||||||
import {dprobeHeartbeat} from './watchers/dprobe/dprobe-heartbeat-watcher'
|
import {dprobeHeartbeat} from './watchers/dprobe/dprobe-heartbeat-watcher'
|
||||||
import {webDiffWatcher} from "./watchers/webdiff/webdiff-watcher";
|
import {webDiffWatcher} from "./watchers/webdiff/webdiff-watcher";
|
||||||
import {ServiceState} from "./types/state";
|
import {Watcher} from "./types/state";
|
||||||
|
|
||||||
export async function dwatch(confFile: string) {
|
export async function dwatch(confFile: string) {
|
||||||
|
|
||||||
const yml = fs.readFileSync(confFile, 'utf8')
|
const yml = fs.readFileSync(confFile, 'utf8')
|
||||||
const conf = yaml.load(yml) as Conf
|
const conf = yaml.load(yml) as Conf
|
||||||
const states: ServiceState[] = []
|
const watchers: Watcher[] = [];
|
||||||
const watchers: () => Promise<>
|
(await Promise.all((conf.ws2pServers || []).map(ws2pWatcher(conf)))).forEach(w => watchers.push(w));
|
||||||
await Promise.all((conf.ws2pServers || []).map(ws2pWatcher(conf)))
|
(await Promise.all((conf.bmaServers || []).map(bmaWatcher(conf)))).forEach(w => watchers.push(w));
|
||||||
await Promise.all((conf.bmaServers || []).map(bmaWatcher(conf)))
|
(await Promise.all((conf.dprobeHeartbeats || []).map(dprobeHeartbeat(conf)))).forEach(w => watchers.push(w));
|
||||||
await Promise.all((conf.dprobeHeartbeats || []).map(dprobeHeartbeat(conf)))
|
(await Promise.all((conf.webDiffServers || []).map(webDiffWatcher(conf)))).forEach(w => watchers.push(w));
|
||||||
await Promise.all((conf.webDiffServers || []).map(webDiffWatcher(conf)))
|
return watchers
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// (await Promise.all((conf.headServers || []).map(headWatcher(conf)))).forEach(w => watchers.push(w));
|
||||||
|
// (await Promise.all((conf.wwMeta || []).map(jsonWatcher(conf)))).forEach(w => watchers.push(w));
|
||||||
@@ -17,6 +17,7 @@ export interface ConfWS2P {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface ConfURL {
|
export interface ConfURL {
|
||||||
|
name: string
|
||||||
address: string
|
address: string
|
||||||
frequency: number
|
frequency: number
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,21 @@
|
|||||||
export interface ServiceState {
|
export class Watcher {
|
||||||
state: 'INIT'|'OK'|'FAILURE'
|
private state: 'INIT'|'OK'|'FAILURE'|'RECOVERED'
|
||||||
}
|
private failureMessage?: string
|
||||||
|
|
||||||
|
constructor(public readonly name: string) {
|
||||||
|
this.state = "INIT"
|
||||||
|
}
|
||||||
|
|
||||||
|
public stateOK() {
|
||||||
|
this.state = "OK"
|
||||||
|
}
|
||||||
|
|
||||||
|
public stateRecovered() {
|
||||||
|
this.state = "RECOVERED"
|
||||||
|
}
|
||||||
|
|
||||||
|
public stateFailure(error: string) {
|
||||||
|
this.state = "FAILURE"
|
||||||
|
this.failureMessage = error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import {ServiceState} from "./types/state";
|
import {Watcher} from "./types/state";
|
||||||
|
|
||||||
export function watcherLoop(
|
export function watcherLoop(
|
||||||
|
name: string,
|
||||||
connect: () => Promise<void>,
|
connect: () => Promise<void>,
|
||||||
onConnectionClosed: () => Promise<void>,
|
onConnectionClosed: () => Promise<void>,
|
||||||
reconnectionDelays: number[],
|
reconnectionDelays: number[],
|
||||||
@@ -9,9 +10,9 @@ export function watcherLoop(
|
|||||||
onRestart: () => Promise<void>,
|
onRestart: () => Promise<void>,
|
||||||
onRestartSuccess: () => Promise<void>,
|
onRestartSuccess: () => Promise<void>,
|
||||||
onError: (e: Error) => Promise<void>,
|
onError: (e: Error) => Promise<void>,
|
||||||
): ServiceState {
|
): Watcher {
|
||||||
|
|
||||||
let state: ServiceState = { state: 'INIT' }
|
let watcher: Watcher = new Watcher(name)
|
||||||
let hasStarted = false
|
let hasStarted = false
|
||||||
|
|
||||||
;(async () => {
|
;(async () => {
|
||||||
@@ -31,8 +32,10 @@ export function watcherLoop(
|
|||||||
if (!hasStarted) {
|
if (!hasStarted) {
|
||||||
hasStarted = true
|
hasStarted = true
|
||||||
await onStart()
|
await onStart()
|
||||||
|
watcher.stateOK()
|
||||||
} else {
|
} else {
|
||||||
await onRestartSuccess()
|
await onRestartSuccess()
|
||||||
|
watcher.stateRecovered()
|
||||||
}
|
}
|
||||||
|
|
||||||
// We reset the delay of reconnection
|
// We reset the delay of reconnection
|
||||||
@@ -42,6 +45,7 @@ export function watcherLoop(
|
|||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
await onError(e)
|
await onError(e)
|
||||||
|
watcher.stateFailure(e && e.message || e)
|
||||||
}
|
}
|
||||||
// Wait before reconnecting
|
// Wait before reconnecting
|
||||||
const waitingDelay = reconnectionDelays[Math.min(reconnectionDelays.length - 1, i)]
|
const waitingDelay = reconnectionDelays[Math.min(reconnectionDelays.length - 1, i)]
|
||||||
@@ -50,4 +54,6 @@ export function watcherLoop(
|
|||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
})()
|
})()
|
||||||
|
|
||||||
|
return watcher
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,15 +2,17 @@ import {watcherLoop} from "../../watcherLoop";
|
|||||||
import {Conf, ConfURL} from "../../types/conf";
|
import {Conf, ConfURL} from "../../types/conf";
|
||||||
import Axios from "axios";
|
import Axios from "axios";
|
||||||
import {mail} from "../../mail";
|
import {mail} from "../../mail";
|
||||||
|
import {Watcher} from "../../types/state";
|
||||||
|
|
||||||
export function urlWatcher(conf: Conf, checkValidity: (data: any) => Promise<void>) {
|
export function urlWatcher(conf: Conf, checkValidity: (data: any) => Promise<void>) {
|
||||||
|
|
||||||
return async (urlConf: ConfURL) => {
|
return async (urlConf: ConfURL): Promise<Watcher> => {
|
||||||
|
|
||||||
let nodeDownRes: () => void
|
let nodeDownRes: () => void
|
||||||
let nodeDownPromise: Promise<void> = new Promise(res => nodeDownRes = res)
|
let nodeDownPromise: Promise<void> = new Promise(res => nodeDownRes = res)
|
||||||
|
|
||||||
await watcherLoop(
|
return watcherLoop(
|
||||||
|
urlConf.name,
|
||||||
async () => {
|
async () => {
|
||||||
let interval: NodeJS.Timer;
|
let interval: NodeJS.Timer;
|
||||||
const res = await Axios.get(urlConf.address)
|
const res = await Axios.get(urlConf.address)
|
||||||
|
|||||||
@@ -8,12 +8,13 @@ export function bmaWatcher(conf: Conf) {
|
|||||||
|
|
||||||
return async (bmaServer: ConfBMA) => {
|
return async (bmaServer: ConfBMA) => {
|
||||||
|
|
||||||
await 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) {
|
if (bmaServer.maxLate && moment().unix() - block.medianTime > bmaServer.maxLate) {
|
||||||
throw 'Server is late'
|
throw 'Server is late'
|
||||||
}
|
}
|
||||||
})({
|
})({
|
||||||
|
name: `BMA ${bmaServer.address}`,
|
||||||
address: bmaServer.address + URL_PATH,
|
address: bmaServer.address + URL_PATH,
|
||||||
frequency: bmaServer.frequency
|
frequency: bmaServer.frequency
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ export function dprobeHeartbeat(conf: Conf) {
|
|||||||
|
|
||||||
return async (dconf: ConfDprobeHeartbeat) => {
|
return async (dconf: ConfDprobeHeartbeat) => {
|
||||||
|
|
||||||
await urlWatcher(conf, async (data) => {
|
return urlWatcher(conf, async (data) => {
|
||||||
const last = moment(data, 'YYYY-MM-DD HH:mm:ss\n')
|
const last = moment(data, 'YYYY-MM-DD HH:mm:ss\n')
|
||||||
const past = moment().diff(last)
|
const past = moment().diff(last)
|
||||||
if (past > dconf.lastBeat) {
|
if (past > dconf.lastBeat) {
|
||||||
|
|||||||
@@ -39,7 +39,8 @@ export function webDiffWatcher(conf: Conf) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await watcherLoop(
|
return watcherLoop(
|
||||||
|
`webdiff ${target}`,
|
||||||
async () => {
|
async () => {
|
||||||
const data1 = await Axios.get(webDiffConf.file1)
|
const data1 = await Axios.get(webDiffConf.file1)
|
||||||
const data2 = await Axios.get(webDiffConf.file2)
|
const data2 = await Axios.get(webDiffConf.file2)
|
||||||
|
|||||||
@@ -16,7 +16,8 @@ export function ws2pWatcher(conf: Conf) {
|
|||||||
const keypair = new Key(keys.pub, keys.sec)
|
const keypair = new Key(keys.pub, keys.sec)
|
||||||
const target = `${wserver.address} (${wserver.expectedPubkey.substr(0, 8)})`
|
const target = `${wserver.address} (${wserver.expectedPubkey.substr(0, 8)})`
|
||||||
|
|
||||||
await watcherLoop(
|
return watcherLoop(
|
||||||
|
`WS2P ${wserver.address}`,
|
||||||
async () => {
|
async () => {
|
||||||
|
|
||||||
const localAuth = new WS2PPubkeyLocalAuth(wserver.currency, keypair, "", async () => true)
|
const localAuth = new WS2PPubkeyLocalAuth(wserver.currency, keypair, "", async () => true)
|
||||||
|
|||||||
@@ -9,7 +9,8 @@
|
|||||||
"noImplicitThis": true,
|
"noImplicitThis": true,
|
||||||
"noImplicitAny": true,
|
"noImplicitAny": true,
|
||||||
"noImplicitReturns": true,
|
"noImplicitReturns": true,
|
||||||
"experimentalDecorators": true
|
"experimentalDecorators": true,
|
||||||
|
"skipLibCheck": true
|
||||||
},
|
},
|
||||||
"include": [
|
"include": [
|
||||||
"src/*",
|
"src/*",
|
||||||
|
|||||||
Reference in New Issue
Block a user