[enh] generalise dwatcher (not only WS2P)

This commit is contained in:
2019-06-08 13:49:16 +02:00
parent d2154c987b
commit 730ee3cbf9
3 changed files with 29 additions and 24 deletions

View File

@@ -15,7 +15,7 @@ reconnectionDelays:
- 21600000 # 6h - 21600000 # 6h
- 43200000 # 12h - 43200000 # 12h
ws2pServers: ws2pServers:
- address: ws://g1-test.cgeek.fr:20900 - address: ws://g1-test.cgeek.fr:20902
expectedPubkey: 2ny7YAdmzReQxAayyJZsyVYwYhVyax2thKcGknmQy5nQ expectedPubkey: 2ny7YAdmzReQxAayyJZsyVYwYhVyax2thKcGknmQy5nQ
mail: mail:
@@ -27,4 +27,4 @@ mail:
username: SMTP_Injection username: SMTP_Injection
apikey: 6976eb51635b680b832dbe4914524d2c038a13b6 apikey: 6976eb51635b680b832dbe4914524d2c038a13b6
from: '"DWatcher" <dwatcher@cgeek.fr>' from: '"DWatcher" <dwatcher@cgeek.fr>'
to: cem.moreau@gmail.com to: cem.moreau@gmail.com

View File

@@ -18,13 +18,15 @@ export async function dwatch(confFile: string) {
await Promise.all(conf.ws2pServers.map(async wserver => { await Promise.all(conf.ws2pServers.map(async wserver => {
let c: WS2PConnection
await processHandler( await processHandler(
() => { async () => {
const localAuth = new WS2PPubkeyLocalAuth(conf.currency, keypair, "", async () => true) const localAuth = new WS2PPubkeyLocalAuth(conf.currency, keypair, "", async () => true)
const remoteAuth = new WS2PPubkeyRemoteAuth(conf.currency, keypair, async () => true) const remoteAuth = new WS2PPubkeyRemoteAuth(conf.currency, keypair, async () => true)
return WS2PConnection.newConnectionToAddress( c = WS2PConnection.newConnectionToAddress(
1, 1,
wserver.address, wserver.address,
new MessageHandler(), new MessageHandler(),
@@ -37,11 +39,15 @@ export async function dwatch(confFile: string) {
}, },
wserver.expectedKey wserver.expectedKey
) )
await c.connectAsInitiator()
}, },
() => c.closed,
conf.reconnectionDelays, conf.reconnectionDelays,
async (c: WS2PConnection) => { async () => {
console.log('Connection established') console.log('Connection established')
await sendMail(conf.mail, '[dwatcher] Connection established', ` await sendMail(conf.mail, '[dwatcher] Connection established', `
<p> <p>
@@ -51,7 +57,7 @@ export async function dwatch(confFile: string) {
}, },
// When a disconnection is detected // When a disconnection is detected
async (c: WS2PConnection, waitingDelay) => { async (waitingDelay) => {
console.log('Connection closed') console.log('Connection closed')
console.log('Waiting %s seconds...', waitingDelay) console.log('Waiting %s seconds...', waitingDelay)
await sendMail(conf.mail, '[dwatcher] Connection closed', ` 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) console.log('Trying to connect to %s', c.pubkey)
}, },
async (c: WS2PConnection) => { async () => {
console.log('Connection recovered') console.log('Connection recovered')
await sendMail(conf.mail, '[dwatcher] Connection recovered', ` await sendMail(conf.mail, '[dwatcher] Connection recovered', `
<p> <p>
@@ -83,4 +89,4 @@ export async function dwatch(confFile: string) {
) )
})) }))
} }

View File

@@ -1,18 +1,18 @@
import {WS2PConnection} from 'duniter/app/modules/ws2p/lib/WS2PConnection' import {WS2PConnection} from 'duniter/app/modules/ws2p/lib/WS2PConnection'
export async function processHandler( export async function processHandler(
getConnection: () => WS2PConnection, connect: () => Promise<void>,
onConnectionClosed: () => Promise<void>,
reconnectionDelays: number[], reconnectionDelays: number[],
onStart: (c: WS2PConnection) => Promise<void>, onStart: () => Promise<void>,
onDisconnection: (c: WS2PConnection, waitingDelay: number) => Promise<void>, onDisconnection: (waitingDelay: number) => Promise<void>,
onRestart: (c: WS2PConnection) => Promise<void>, onRestart: () => Promise<void>,
onRestartSuccess: (c: WS2PConnection) => Promise<void>, onRestartSuccess: () => Promise<void>,
onError: (e: Error) => Promise<void>, onError: (e: Error) => Promise<void>,
) { ) {
let hasStarted = false let hasStarted = false
let connection = getConnection() let connecting = await connect()
let connecting = connection.connectAsInitiator()
;(async () => { ;(async () => {
@@ -22,9 +22,8 @@ export async function processHandler(
try { try {
if (hasStarted) { if (hasStarted) {
await onRestart(connection) await onRestart()
connection = getConnection() connecting = await connect()
connecting = connection.connectAsInitiator()
} }
// Connection trial // Connection trial
@@ -32,24 +31,24 @@ export async function processHandler(
if (!hasStarted) { if (!hasStarted) {
hasStarted = true hasStarted = true
await onStart(connection) await onStart()
} else { } else {
await onRestartSuccess(connection) await onRestartSuccess()
} }
// We reset the delay of reconnection // We reset the delay of reconnection
i = 0 i = 0
await connection.closed await onConnectionClosed()
} catch (e) { } catch (e) {
await onError(e) await onError(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)]
await onDisconnection(connection, waitingDelay) await onDisconnection(waitingDelay)
await new Promise(resolve => setTimeout(resolve, waitingDelay)) await new Promise(resolve => setTimeout(resolve, waitingDelay))
i++ i++
} }
})() })()
} }