mailing: added queue
This commit is contained in:
100
src/lib/mail.ts
100
src/lib/mail.ts
@@ -4,47 +4,85 @@ import {ConfMail} from './types/conf'
|
||||
import * as nodemailer from 'nodemailer'
|
||||
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] Body: ${html}`)
|
||||
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,
|
||||
cc: (conf.cc ? [conf.cc] : []).concat(cc ? [cc] : []).join(','),
|
||||
subject,
|
||||
html
|
||||
})
|
||||
if (!conf.enabled) {
|
||||
console.warn(`Mail is disabled.`)
|
||||
return
|
||||
}
|
||||
|
||||
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,
|
||||
cc: (conf.cc ? [conf.cc] : []).concat(cc ? [cc] : []).join(','),
|
||||
subject,
|
||||
html
|
||||
})
|
||||
}
|
||||
|
||||
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>
|
||||
Connection from [${os.hostname}] to ${target} established on ${moment().format('DD-MM-YYYY HH:mm:ss')}.
|
||||
</p>
|
||||
`) => {
|
||||
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>
|
||||
Connection from [${os.hostname}] to ${target} was lost on ${moment().format('dd-MM-YYYY HH:mm:ss')}.
|
||||
</p>
|
||||
@@ -55,18 +93,24 @@ export const mail = {
|
||||
`) => {
|
||||
return async (waitingDelay: number, recallDelay: number, cc?: string) => {
|
||||
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>
|
||||
Connection from [${os.hostname}] to ${target} was recovered on ${moment().format('dd-MM-YYYY HH:mm:ss')}.
|
||||
</p>
|
||||
`) => {
|
||||
return async (cc?: string) => {
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user