From 356a07e39e82c0116f058df0cdd6ada78adca699 Mon Sep 17 00:00:00 2001 From: cgeek Date: Sun, 20 Sep 2020 11:08:00 +0200 Subject: [PATCH] mailing: frequency consumption set by conf --- app.yml | 1 + src/lib/mail.ts | 18 ++++++++++-------- src/lib/types/conf.ts | 1 + 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/app.yml b/app.yml index f3ac6fa..4fd55f9 100644 --- a/app.yml +++ b/app.yml @@ -38,6 +38,7 @@ wwMeta: mail: enabled: false + frequency: 10000 # 10" host: smtp.sparkpostmail.com port: 587 auth: LOGIN diff --git a/src/lib/mail.ts b/src/lib/mail.ts index 60578bb..d978439 100644 --- a/src/lib/mail.ts +++ b/src/lib/mail.ts @@ -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 diff --git a/src/lib/types/conf.ts b/src/lib/types/conf.ts index 99b8a80..ef1c7e5 100644 --- a/src/lib/types/conf.ts +++ b/src/lib/types/conf.ts @@ -51,6 +51,7 @@ export interface ConfDprobeHeartbeat extends ConfURL{ export interface ConfMail { enabled: boolean + frequency: number host: string port: number auth: string