[enh] Auto-restart
This commit is contained in:
@@ -7,6 +7,7 @@ import {Key} from 'duniter/app/lib/common-libs/crypto/keyring'
|
||||
import {Scrypt} from 'duniter/app/modules/keypair/lib/scrypt'
|
||||
import {sendMail} from './sendMail'
|
||||
import {moment} from 'duniter/app/lib/common-libs/moment'
|
||||
import {processHandler} from './processHandler'
|
||||
|
||||
export async function dwatch(confFile: string) {
|
||||
|
||||
@@ -17,36 +18,69 @@ export async function dwatch(confFile: string) {
|
||||
|
||||
await Promise.all(conf.ws2pServers.map(async wserver => {
|
||||
|
||||
const localAuth = new WS2PPubkeyLocalAuth(conf.currency, keypair, "", async () => true)
|
||||
const remoteAuth = new WS2PPubkeyRemoteAuth(conf.currency, keypair, async () => true)
|
||||
await processHandler(
|
||||
() => {
|
||||
|
||||
const c = WS2PConnection.newConnectionToAddress(
|
||||
1,
|
||||
wserver.address,
|
||||
new MessageHandler(),
|
||||
localAuth,
|
||||
remoteAuth,
|
||||
undefined,
|
||||
{
|
||||
connectionTimeout: conf.connectionTimeout,
|
||||
requestTimeout: 0 // No request anyway
|
||||
const localAuth = new WS2PPubkeyLocalAuth(conf.currency, keypair, "", async () => true)
|
||||
const remoteAuth = new WS2PPubkeyRemoteAuth(conf.currency, keypair, async () => true)
|
||||
|
||||
return WS2PConnection.newConnectionToAddress(
|
||||
1,
|
||||
wserver.address,
|
||||
new MessageHandler(),
|
||||
localAuth,
|
||||
remoteAuth,
|
||||
undefined,
|
||||
{
|
||||
connectionTimeout: conf.connectionTimeout,
|
||||
requestTimeout: 0 // No request anyway
|
||||
},
|
||||
wserver.expectedKey
|
||||
)
|
||||
},
|
||||
wserver.expectedKey
|
||||
)
|
||||
|
||||
await c.connectAsInitiator()
|
||||
conf.reconnectionDelays,
|
||||
|
||||
c.closed.then(async () => {
|
||||
console.log('Connection closed')
|
||||
await sendMail(conf.mail, '[dwatcher] Connection closed', `
|
||||
async (c: WS2PConnection) => {
|
||||
console.log('Connection established')
|
||||
await sendMail(conf.mail, '[dwatcher] Connection established', `
|
||||
<p>
|
||||
Connection to ${c.pubkey} established on ${moment().format('DD-MM-YYYY HH:mm:ss')}.
|
||||
</p>
|
||||
`)
|
||||
},
|
||||
|
||||
// When a disconnection is detected
|
||||
async (c: WS2PConnection, waitingDelay) => {
|
||||
console.log('Connection closed')
|
||||
console.log('Waiting %s seconds...', waitingDelay)
|
||||
await sendMail(conf.mail, '[dwatcher] Connection closed', `
|
||||
<p>
|
||||
Connection to ${c.pubkey} was lost on ${moment().format('dd-MM-YYYY HH:mm:ss')}.
|
||||
</p>
|
||||
<p>
|
||||
Waiting ${(waitingDelay / 1000).toFixed(0)} seconds before trying to reconnect.
|
||||
</p>
|
||||
`)
|
||||
},
|
||||
|
||||
async (c: WS2PConnection) => {
|
||||
console.log('Trying to connect to %s', c.pubkey)
|
||||
},
|
||||
|
||||
async (c: WS2PConnection) => {
|
||||
console.log('Connection recovered')
|
||||
await sendMail(conf.mail, '[dwatcher] Connection recovered', `
|
||||
<p>
|
||||
Connection to ${c.pubkey} was lost on ${moment().format('dd-MM-YYYY HH:mm:ss')}.
|
||||
</p>
|
||||
`)
|
||||
})
|
||||
},
|
||||
|
||||
await c.connected
|
||||
async (e) => {
|
||||
console.error(e)
|
||||
}
|
||||
)
|
||||
|
||||
console.log('Connected to %s', c.pubkey)
|
||||
}))
|
||||
}
|
||||
Reference in New Issue
Block a user