mailing: frequency consumption set by conf
This commit is contained in:
1
app.yml
1
app.yml
@@ -38,6 +38,7 @@ wwMeta:
|
|||||||
|
|
||||||
mail:
|
mail:
|
||||||
enabled: false
|
enabled: false
|
||||||
|
frequency: 10000 # 10"
|
||||||
host: smtp.sparkpostmail.com
|
host: smtp.sparkpostmail.com
|
||||||
port: 587
|
port: 587
|
||||||
auth: LOGIN
|
auth: LOGIN
|
||||||
|
|||||||
@@ -1,31 +1,33 @@
|
|||||||
import {moment} from "duniter/app/lib/common-libs/moment";
|
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 nodemailer from 'nodemailer'
|
||||||
import * as os from 'os'
|
import * as os from 'os'
|
||||||
|
|
||||||
// TODO: in confMail
|
const DEFAULT_FREQUENCY = 5 * 60 * 1000 // 5'
|
||||||
|
|
||||||
const QUEUE_PERIOD = 5000
|
|
||||||
const queue: EmailContent[] = []
|
const queue: EmailContent[] = []
|
||||||
let conf: ConfMail|undefined
|
let conf: ConfMail|undefined
|
||||||
|
|
||||||
export function initConfMail(confMail: ConfMail) {
|
export function initConfMail(confMail: ConfMail) {
|
||||||
conf = 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 () => {
|
;(async () => {
|
||||||
while (true) {
|
while (true) {
|
||||||
await consumeQueue()
|
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) {
|
export async function queueEmail(subject: string, html: string, cc?: string) {
|
||||||
queue.push({ subject, body: html, cc })
|
queue.push({ subject, body: html, cc })
|
||||||
|
console.log(`[mail] added 1 mail to queue`)
|
||||||
}
|
}
|
||||||
|
|
||||||
async function consumeQueue() {
|
async function consumeQueue() {
|
||||||
|
console.log(`[mail] consuming mailing queue: ${queue.length} messages to be sent`)
|
||||||
while (queue.length) {
|
while (queue.length) {
|
||||||
const mail = queue.shift()
|
const mail = queue.shift()
|
||||||
if (mail) {
|
if (mail) {
|
||||||
@@ -41,8 +43,8 @@ async function sendEmail(subject: string, html: string, cc?: string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`[mail] Subject: ${subject}`)
|
console.debug(`[mail] Subject: ${subject}`)
|
||||||
console.log(`[mail] Body: ${html}`)
|
console.debug(`[mail] Body: ${html}`)
|
||||||
if (!conf.enabled) {
|
if (!conf.enabled) {
|
||||||
console.warn(`Mail is disabled.`)
|
console.warn(`Mail is disabled.`)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -51,6 +51,7 @@ export interface ConfDprobeHeartbeat extends ConfURL{
|
|||||||
|
|
||||||
export interface ConfMail {
|
export interface ConfMail {
|
||||||
enabled: boolean
|
enabled: boolean
|
||||||
|
frequency: number
|
||||||
host: string
|
host: string
|
||||||
port: number
|
port: number
|
||||||
auth: string
|
auth: string
|
||||||
|
|||||||
Reference in New Issue
Block a user