[enh] add BMA watcher + reorganize code

This commit is contained in:
2019-06-08 18:34:39 +02:00
parent d02de7842f
commit 5544d56ebb
13 changed files with 232 additions and 606 deletions

69
src/lib/mail.ts Normal file
View File

@@ -0,0 +1,69 @@
import {moment} from "duniter/app/lib/common-libs/moment";
import {Conf} from "./types/conf";
import {ConfMail} from './types/conf'
import * as nodemailer from 'nodemailer'
export async function sendMail(conf: ConfMail, subject: string, html: string) {
if (conf.enabled) {
let transporter = nodemailer.createTransport({
host: conf.host,
port: conf.port,
secure: false, // true for 465, false for other ports
auth: {
user: conf.username, // generated ethereal user
pass: conf.apikey // generated ethereal password
},
authMethod: conf.auth,
requireTLS: true,
});
// send mail with defined transport object
let info = await transporter.sendMail({
from: conf.from,
to: conf.to,
subject,
html
})
}
}
export const mail = {
onEstablished: (conf: Conf, target: string) => {
return async () => {
console.log('Connection established')
await sendMail(conf.mail, '[dwatcher] Connection established', `
<p>
Connection to ${target} established on ${moment().format('DD-MM-YYYY HH:mm:ss')}.
</p>
`)
}
},
onDisconnect: (conf: Conf, target: string) => {
return async (waitingDelay: number) => {
console.log('Connection closed')
console.log('Waiting %s seconds...', waitingDelay)
await sendMail(conf.mail, '[dwatcher] Connection closed', `
<p>
Connection to ${target} was lost on ${moment().format('dd-MM-YYYY HH:mm:ss')}.
</p>
<p>
Waiting ${(waitingDelay / 1000).toFixed(0)} seconds before trying to reconnect.
</p>
`)
}
},
onRestartSuccess: (conf: Conf, target: string) => {
return async () => {
console.log('Connection recovered')
await sendMail(conf.mail, '[dwatcher] Connection recovered', `
<p>
Connection to ${target} was lost on ${moment().format('dd-MM-YYYY HH:mm:ss')}.
</p>
`)
}
}
}