mailing: added queue
This commit is contained in:
@@ -8,11 +8,13 @@ import {webDiffWatcher} from "./watchers/webdiff/webdiff-watcher";
|
|||||||
import {Watcher} from "./types/state";
|
import {Watcher} from "./types/state";
|
||||||
import {headWatcher} from "./watchers/bma/head-watcher";
|
import {headWatcher} from "./watchers/bma/head-watcher";
|
||||||
import {jsonWatcher} from "./watchers/wotwizard/json-watcher";
|
import {jsonWatcher} from "./watchers/wotwizard/json-watcher";
|
||||||
|
import {initConfMail} from './mail'
|
||||||
|
|
||||||
export async function dwatch(confFile: string) {
|
export async function dwatch(confFile: string) {
|
||||||
|
|
||||||
const yml = fs.readFileSync(confFile, 'utf8')
|
const yml = fs.readFileSync(confFile, 'utf8')
|
||||||
const conf = yaml.load(yml) as Conf
|
const conf = yaml.load(yml) as Conf
|
||||||
|
initConfMail(conf.mail)
|
||||||
const watchers: Watcher[] = [];
|
const watchers: Watcher[] = [];
|
||||||
(await Promise.all((conf.ws2pServers || []).map(ws2pWatcher(conf)))).forEach(w => watchers.push(w));
|
(await Promise.all((conf.ws2pServers || []).map(ws2pWatcher(conf)))).forEach(w => watchers.push(w));
|
||||||
(await Promise.all((conf.bmaServers || []).map(bmaWatcher(conf)))).forEach(w => watchers.push(w));
|
(await Promise.all((conf.bmaServers || []).map(bmaWatcher(conf)))).forEach(w => watchers.push(w));
|
||||||
|
|||||||
@@ -4,11 +4,50 @@ import {ConfMail} from './types/conf'
|
|||||||
import * as nodemailer from 'nodemailer'
|
import * as nodemailer from 'nodemailer'
|
||||||
import * as os from 'os'
|
import * as os from 'os'
|
||||||
|
|
||||||
export async function sendMail(conf: ConfMail, subject: string, html: string, cc?: string) {
|
// TODO: in confMail
|
||||||
|
|
||||||
|
const QUEUE_PERIOD = 5000
|
||||||
|
const queue: EmailContent[] = []
|
||||||
|
let conf: ConfMail|undefined
|
||||||
|
|
||||||
|
export function initConfMail(confMail: ConfMail) {
|
||||||
|
conf = confMail
|
||||||
|
|
||||||
|
;(async () => {
|
||||||
|
while (true) {
|
||||||
|
await consumeQueue()
|
||||||
|
await new Promise((res) => setTimeout(res, QUEUE_PERIOD))
|
||||||
|
}
|
||||||
|
})()
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function queueEmail(subject: string, html: string, cc?: string) {
|
||||||
|
queue.push({ subject, body: html, cc })
|
||||||
|
}
|
||||||
|
|
||||||
|
async function consumeQueue() {
|
||||||
|
while (queue.length) {
|
||||||
|
const mail = queue.shift()
|
||||||
|
if (mail) {
|
||||||
|
await sendEmail(mail.subject, mail.body, mail.cc)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function sendEmail(subject: string, html: string, cc?: string) {
|
||||||
|
|
||||||
|
if (!conf) {
|
||||||
|
console.error(`Mail is not properly configured.`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
console.log(`[mail] Subject: ${subject}`)
|
console.log(`[mail] Subject: ${subject}`)
|
||||||
console.log(`[mail] Body: ${html}`)
|
console.log(`[mail] Body: ${html}`)
|
||||||
if (conf.enabled) {
|
if (!conf.enabled) {
|
||||||
|
console.warn(`Mail is disabled.`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
let transporter = nodemailer.createTransport({
|
let transporter = nodemailer.createTransport({
|
||||||
host: conf.host,
|
host: conf.host,
|
||||||
port: conf.port,
|
port: conf.port,
|
||||||
@@ -30,21 +69,20 @@ export async function sendMail(conf: ConfMail, subject: string, html: string, cc
|
|||||||
html
|
html
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
export const mail = {
|
export const mail = {
|
||||||
|
|
||||||
onEstablished: (conf: Conf, target: string, message = `Connection established for ${target}`, getHtml: () => string = () => `
|
onEstablished: (target: string, message = `Connection established for ${target}`, getHtml: () => string = () => `
|
||||||
<p>
|
<p>
|
||||||
Connection from [${os.hostname}] to ${target} established on ${moment().format('DD-MM-YYYY HH:mm:ss')}.
|
Connection from [${os.hostname}] to ${target} established on ${moment().format('DD-MM-YYYY HH:mm:ss')}.
|
||||||
</p>
|
</p>
|
||||||
`) => {
|
`) => {
|
||||||
return async (cc?: string) => {
|
return async (cc?: string) => {
|
||||||
await sendMail(conf.mail, `[dw] [${os.hostname}] ${message}`, getHtml(), cc)
|
await queueEmail(`[dw] [${os.hostname}] ${message}`, getHtml(), cc)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
onDisconnect: (conf: Conf, target: string, message = `Connection closed for ${target}`, getErrorMessage: () => string = () => '', getHtml: (waitingDelay: number, recallDelay: number) => string = (waitingDelay: number, recallDelay: number) => `
|
onDisconnect: (target: string, message = `Connection closed for ${target}`, getErrorMessage: () => string = () => '', getHtml: (waitingDelay: number, recallDelay: number) => string = (waitingDelay: number, recallDelay: number) => `
|
||||||
<p>
|
<p>
|
||||||
Connection from [${os.hostname}] to ${target} was lost on ${moment().format('dd-MM-YYYY HH:mm:ss')}.
|
Connection from [${os.hostname}] to ${target} was lost on ${moment().format('dd-MM-YYYY HH:mm:ss')}.
|
||||||
</p>
|
</p>
|
||||||
@@ -55,18 +93,24 @@ export const mail = {
|
|||||||
`) => {
|
`) => {
|
||||||
return async (waitingDelay: number, recallDelay: number, cc?: string) => {
|
return async (waitingDelay: number, recallDelay: number, cc?: string) => {
|
||||||
console.log('Waiting %s seconds...', (waitingDelay / 1000).toFixed(0))
|
console.log('Waiting %s seconds...', (waitingDelay / 1000).toFixed(0))
|
||||||
await sendMail(conf.mail, `[dw] [${os.hostname}] ${message}`, getHtml(waitingDelay, recallDelay), cc)
|
await queueEmail(`[dw] [${os.hostname}] ${message}`, getHtml(waitingDelay, recallDelay), cc)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
onRestartSuccess: (conf: Conf, target: string, message = `Connection recovered for ${target}`, getHtml: () => string = () => `
|
onRestartSuccess: (target: string, message = `Connection recovered for ${target}`, getHtml: () => string = () => `
|
||||||
<p>
|
<p>
|
||||||
Connection from [${os.hostname}] to ${target} was recovered on ${moment().format('dd-MM-YYYY HH:mm:ss')}.
|
Connection from [${os.hostname}] to ${target} was recovered on ${moment().format('dd-MM-YYYY HH:mm:ss')}.
|
||||||
</p>
|
</p>
|
||||||
`) => {
|
`) => {
|
||||||
return async (cc?: string) => {
|
return async (cc?: string) => {
|
||||||
console.log(`${message}`)
|
console.log(`${message}`)
|
||||||
await sendMail(conf.mail, `[dw] [${os.hostname}] ${message}`, getHtml(), cc)
|
await queueEmail(`[dw] [${os.hostname}] ${message}`, getHtml(), cc)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface EmailContent {
|
||||||
|
subject: string
|
||||||
|
body: string
|
||||||
|
cc?: string
|
||||||
|
}
|
||||||
@@ -47,7 +47,7 @@ export function urlWatcher(conf: Conf, checkValidity: (data: any) => Promise<Url
|
|||||||
conf.waitingDelay,
|
conf.waitingDelay,
|
||||||
conf.recallDelay,
|
conf.recallDelay,
|
||||||
|
|
||||||
mail.onEstablished(conf, urlConf.address, getOkTitle()),
|
mail.onEstablished(urlConf.address, getOkTitle()),
|
||||||
|
|
||||||
// When a disconnection is detected
|
// When a disconnection is detected
|
||||||
(waitingDelay: number, recallDelay, error?: any) => {
|
(waitingDelay: number, recallDelay, error?: any) => {
|
||||||
@@ -57,14 +57,14 @@ export function urlWatcher(conf: Conf, checkValidity: (data: any) => Promise<Url
|
|||||||
koTitle = getKoTitle()
|
koTitle = getKoTitle()
|
||||||
koMessage = () => `<p>${error.errorMessage}</p>`
|
koMessage = () => `<p>${error.errorMessage}</p>`
|
||||||
}
|
}
|
||||||
return mail.onDisconnect(conf, urlConf.address, koTitle, koMessage)(waitingDelay, recallDelay)
|
return mail.onDisconnect(urlConf.address, koTitle, koMessage)(waitingDelay, recallDelay)
|
||||||
},
|
},
|
||||||
|
|
||||||
async () => {
|
async () => {
|
||||||
console.log('Trying to connect to %s', urlConf.address)
|
console.log('Trying to connect to %s', urlConf.address)
|
||||||
},
|
},
|
||||||
|
|
||||||
mail.onRestartSuccess(conf, urlConf.address, getRecoveredTitle()),
|
mail.onRestartSuccess(urlConf.address, getRecoveredTitle()),
|
||||||
)
|
)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,10 +64,10 @@ export function webDiffWatcher(conf: Conf) {
|
|||||||
conf.waitingDelay,
|
conf.waitingDelay,
|
||||||
conf.recallDelay,
|
conf.recallDelay,
|
||||||
|
|
||||||
() => mail.onEstablished(conf, target, 'webdiff successfully started')(webDiffConf.cc),
|
() => mail.onEstablished(target, 'webdiff successfully started')(webDiffConf.cc),
|
||||||
|
|
||||||
// When a disconnection is detected
|
// When a disconnection is detected
|
||||||
(waitingDelay: number, recallDelay: number) => mail.onDisconnect(conf, target, 'Diff detected', undefined, (waitingDelay: number) => `
|
(waitingDelay: number, recallDelay: number) => mail.onDisconnect(target, 'Diff detected', undefined, (waitingDelay: number) => `
|
||||||
${htmlDiff}
|
${htmlDiff}
|
||||||
<p>
|
<p>
|
||||||
Waiting ${(waitingDelay / 1000).toFixed(0)} seconds before trying to reconnect.
|
Waiting ${(waitingDelay / 1000).toFixed(0)} seconds before trying to reconnect.
|
||||||
@@ -78,7 +78,7 @@ export function webDiffWatcher(conf: Conf) {
|
|||||||
console.log('Trying to connect to %s', target)
|
console.log('Trying to connect to %s', target)
|
||||||
},
|
},
|
||||||
|
|
||||||
() => mail.onRestartSuccess(conf, target)(webDiffConf.cc),
|
() => mail.onRestartSuccess(target)(webDiffConf.cc),
|
||||||
)
|
)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,16 +44,16 @@ export function ws2pWatcher(conf: Conf) {
|
|||||||
conf.waitingDelay,
|
conf.waitingDelay,
|
||||||
conf.recallDelay,
|
conf.recallDelay,
|
||||||
|
|
||||||
mail.onEstablished(conf, target, `State OK WS2P on ${wserver.address}`),
|
mail.onEstablished(target, `State OK WS2P on ${wserver.address}`),
|
||||||
|
|
||||||
// When a disconnection is detected
|
// When a disconnection is detected
|
||||||
mail.onDisconnect(conf, target, `State FAILURE WS2P on ${wserver.address}`),
|
mail.onDisconnect(target, `State FAILURE WS2P on ${wserver.address}`),
|
||||||
|
|
||||||
async () => {
|
async () => {
|
||||||
console.log('Trying to connect to %s', target)
|
console.log('Trying to connect to %s', target)
|
||||||
},
|
},
|
||||||
|
|
||||||
mail.onRestartSuccess(conf, target, `State RECOVERED WS2P on ${wserver.address}`),
|
mail.onRestartSuccess(target, `State RECOVERED WS2P on ${wserver.address}`),
|
||||||
)
|
)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user