[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
- 43200000 # 12h
ws2pServers:
- address: ws://g1-test.cgeek.fr:20900
- address: ws://g1-test.cgeek.fr:20902
expectedPubkey: 2ny7YAdmzReQxAayyJZsyVYwYhVyax2thKcGknmQy5nQ
mail:
@@ -27,4 +27,4 @@ mail:
username: SMTP_Injection
apikey: 6976eb51635b680b832dbe4914524d2c038a13b6
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 => {
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) {
)
}))
}
}

View File

@@ -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++
}
})()
}
}