[enh] generalise dwatcher (not only WS2P)
This commit is contained in:
@@ -18,13 +18,15 @@ export async function dwatch(confFile: string) {
|
||||
|
||||
await Promise.all(conf.ws2pServers.map(async wserver => {
|
||||
|
||||
let c: WS2PConnection
|
||||
|
||||
await processHandler(
|
||||
() => {
|
||||
async () => {
|
||||
|
||||
const localAuth = new WS2PPubkeyLocalAuth(conf.currency, keypair, "", async () => true)
|
||||
const remoteAuth = new WS2PPubkeyRemoteAuth(conf.currency, keypair, async () => true)
|
||||
|
||||
return WS2PConnection.newConnectionToAddress(
|
||||
c = WS2PConnection.newConnectionToAddress(
|
||||
1,
|
||||
wserver.address,
|
||||
new MessageHandler(),
|
||||
@@ -37,11 +39,15 @@ export async function dwatch(confFile: string) {
|
||||
},
|
||||
wserver.expectedKey
|
||||
)
|
||||
|
||||
await c.connectAsInitiator()
|
||||
},
|
||||
|
||||
() => c.closed,
|
||||
|
||||
conf.reconnectionDelays,
|
||||
|
||||
async (c: WS2PConnection) => {
|
||||
async () => {
|
||||
console.log('Connection established')
|
||||
await sendMail(conf.mail, '[dwatcher] Connection established', `
|
||||
<p>
|
||||
@@ -51,7 +57,7 @@ export async function dwatch(confFile: string) {
|
||||
},
|
||||
|
||||
// When a disconnection is detected
|
||||
async (c: WS2PConnection, waitingDelay) => {
|
||||
async (waitingDelay) => {
|
||||
console.log('Connection closed')
|
||||
console.log('Waiting %s seconds...', waitingDelay)
|
||||
await sendMail(conf.mail, '[dwatcher] Connection closed', `
|
||||
@@ -64,11 +70,11 @@ export async function dwatch(confFile: string) {
|
||||
`)
|
||||
},
|
||||
|
||||
async (c: WS2PConnection) => {
|
||||
async () => {
|
||||
console.log('Trying to connect to %s', c.pubkey)
|
||||
},
|
||||
|
||||
async (c: WS2PConnection) => {
|
||||
async () => {
|
||||
console.log('Connection recovered')
|
||||
await sendMail(conf.mail, '[dwatcher] Connection recovered', `
|
||||
<p>
|
||||
@@ -83,4 +89,4 @@ export async function dwatch(confFile: string) {
|
||||
)
|
||||
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
import {WS2PConnection} from 'duniter/app/modules/ws2p/lib/WS2PConnection'
|
||||
|
||||
export async function processHandler(
|
||||
getConnection: () => WS2PConnection,
|
||||
connect: () => Promise<void>,
|
||||
onConnectionClosed: () => Promise<void>,
|
||||
reconnectionDelays: number[],
|
||||
onStart: (c: WS2PConnection) => Promise<void>,
|
||||
onDisconnection: (c: WS2PConnection, waitingDelay: number) => Promise<void>,
|
||||
onRestart: (c: WS2PConnection) => Promise<void>,
|
||||
onRestartSuccess: (c: WS2PConnection) => Promise<void>,
|
||||
onStart: () => Promise<void>,
|
||||
onDisconnection: (waitingDelay: number) => Promise<void>,
|
||||
onRestart: () => Promise<void>,
|
||||
onRestartSuccess: () => Promise<void>,
|
||||
onError: (e: Error) => Promise<void>,
|
||||
) {
|
||||
|
||||
let hasStarted = false
|
||||
let connection = getConnection()
|
||||
let connecting = connection.connectAsInitiator()
|
||||
let connecting = await connect()
|
||||
|
||||
;(async () => {
|
||||
|
||||
@@ -22,9 +22,8 @@ export async function processHandler(
|
||||
try {
|
||||
|
||||
if (hasStarted) {
|
||||
await onRestart(connection)
|
||||
connection = getConnection()
|
||||
connecting = connection.connectAsInitiator()
|
||||
await onRestart()
|
||||
connecting = await connect()
|
||||
}
|
||||
|
||||
// Connection trial
|
||||
@@ -32,24 +31,24 @@ export async function processHandler(
|
||||
|
||||
if (!hasStarted) {
|
||||
hasStarted = true
|
||||
await onStart(connection)
|
||||
await onStart()
|
||||
} else {
|
||||
await onRestartSuccess(connection)
|
||||
await onRestartSuccess()
|
||||
}
|
||||
|
||||
// We reset the delay of reconnection
|
||||
i = 0
|
||||
|
||||
await connection.closed
|
||||
await onConnectionClosed()
|
||||
|
||||
} catch (e) {
|
||||
await onError(e)
|
||||
}
|
||||
// Wait before reconnecting
|
||||
const waitingDelay = reconnectionDelays[Math.min(reconnectionDelays.length - 1, i)]
|
||||
await onDisconnection(connection, waitingDelay)
|
||||
await onDisconnection(waitingDelay)
|
||||
await new Promise(resolve => setTimeout(resolve, waitingDelay))
|
||||
i++
|
||||
}
|
||||
})()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user