feature: surveillance GDEV via WebSocket

This commit is contained in:
Cédric Moreau
2022-05-29 12:08:20 +02:00
parent 921e9e7145
commit 571248ffc4
6 changed files with 15676 additions and 13 deletions

View File

@@ -10,6 +10,7 @@ import {headWatcher} from "./watchers/bma/head-watcher";
import {jsonWatcher} from "./watchers/wotwizard/json-watcher";
import {initConfMail} from './mail'
import {membershipWatcher} from './watchers/bma/membership-watcher'
import {websocketWatcher} from "./watchers/websocket/websocket-watcher";
export async function dwatch(confFile: string) {
@@ -17,6 +18,7 @@ export async function dwatch(confFile: string) {
const conf = yaml.load(yml) as Conf
initConfMail(conf.mail)
const watchers: Watcher[] = [];
(await Promise.all((conf.websocketServers || []).map(websocketWatcher(conf)))).forEach(w => watchers.push(w));
(await Promise.all((conf.ws2pServers || []).map(ws2pWatcher(conf)))).forEach(w => watchers.push(w));
(await Promise.all((conf.bmaServers || []).map(bmaWatcher(conf)))).forEach(w => watchers.push(w));
(await Promise.all((conf.dprobeHeartbeats || []).map(dprobeHeartbeat(conf)))).forEach(w => watchers.push(w));

View File

@@ -3,6 +3,7 @@ export interface Conf {
waitingDelay: number
recallDelay: number
ws2pServers: ConfWS2P[]
websocketServers: ConfWebSocket[]
bmaServers: ConfBMA[]
dprobeHeartbeats: ConfDprobeHeartbeat[]
webDiffServers: ConfWebDiff[]
@@ -12,6 +13,11 @@ export interface Conf {
mail: ConfMail
}
export interface ConfWebSocket {
name: string
address: string
}
export interface ConfWS2P {
address: string
expectedPubkey: string

View File

@@ -0,0 +1,55 @@
import {watcherLoop} from "../../watcherLoop";
import {Conf, ConfWebSocket} from "../../types/conf";
import {mail} from "../../mail";
import * as WebSocket from "ws"
export function websocketWatcher(conf: Conf) {
let c: WebSocket
let closed: Promise<any>
let closedCallback: () => any
return async (wserver: ConfWebSocket) => {
const target = `${wserver.address} (${wserver.name})`
return watcherLoop(
`WebSocket ${wserver.name}`,
async () => {
c = new WebSocket(wserver.address)
closed = new Promise(res => closedCallback = res)
await new Promise((resolve, reject) => {
c.onopen = () => {
console.log('Connecté à ' + wserver.address)
resolve()
}
c.onerror = (e: any) => {
console.log('Erreur de connexion à ' + wserver.address + ' : ' + e.message)
reject()
}
c.onclose = closedCallback
})
console.log('Loop terminé WS')
},
() => closed,
conf.waitingDelay,
conf.recallDelay,
mail.onEstablished(target, () => `State OK WebSocket on ${wserver.address}`),
// When a disconnection is detected
mail.onDisconnect(target, () => `State FAILURE WebSocket on ${wserver.address}`),
async () => {
console.log('Trying to connect to %s', target)
},
mail.onRestartSuccess(target, () => `State RECOVERED WebSocket on ${wserver.address}`),
)
}
}