mailing: frequency consumption set by conf

This commit is contained in:
2020-09-20 11:08:00 +02:00
parent 2cfc050391
commit 356a07e39e
3 changed files with 12 additions and 8 deletions

View File

@@ -38,6 +38,7 @@ wwMeta:
mail:
enabled: false
frequency: 10000 # 10"
host: smtp.sparkpostmail.com
port: 587
auth: LOGIN

View File

@@ -1,31 +1,33 @@
import {moment} from "duniter/app/lib/common-libs/moment";
import {Conf} from "./types/conf";
import {ConfMail} from './types/conf'
import {ConfMail} from "./types/conf";
import * as nodemailer from 'nodemailer'
import * as os from 'os'
// TODO: in confMail
const QUEUE_PERIOD = 5000
const DEFAULT_FREQUENCY = 5 * 60 * 1000 // 5'
const queue: EmailContent[] = []
let conf: ConfMail|undefined
export function initConfMail(confMail: ConfMail) {
conf = confMail
const freq = conf && conf.frequency || DEFAULT_FREQUENCY
const freqS = freq / 1000
console.log(`[conf] mails are sent every ${freqS.toFixed(0)}s`)
;(async () => {
while (true) {
await consumeQueue()
await new Promise((res) => setTimeout(res, QUEUE_PERIOD))
await new Promise((res) => setTimeout(res, freq))
}
})()
}
export async function queueEmail(subject: string, html: string, cc?: string) {
queue.push({ subject, body: html, cc })
console.log(`[mail] added 1 mail to queue`)
}
async function consumeQueue() {
console.log(`[mail] consuming mailing queue: ${queue.length} messages to be sent`)
while (queue.length) {
const mail = queue.shift()
if (mail) {
@@ -41,8 +43,8 @@ async function sendEmail(subject: string, html: string, cc?: string) {
return
}
console.log(`[mail] Subject: ${subject}`)
console.log(`[mail] Body: ${html}`)
console.debug(`[mail] Subject: ${subject}`)
console.debug(`[mail] Body: ${html}`)
if (!conf.enabled) {
console.warn(`Mail is disabled.`)
return

View File

@@ -51,6 +51,7 @@ export interface ConfDprobeHeartbeat extends ConfURL{
export interface ConfMail {
enabled: boolean
frequency: number
host: string
port: number
auth: string