|
|
|
|
@@ -1,61 +1,80 @@
|
|
|
|
|
import { Queue, Worker, QueueEvents } from 'bullmq'
|
|
|
|
|
import { EmailService } from '../services/email'
|
|
|
|
|
import { config } from '../config/env'
|
|
|
|
|
import Redis from 'ioredis'
|
|
|
|
|
import { createTransport } from "nodemailer";
|
|
|
|
|
import { Queue, Worker, QueueEvents } from "bullmq";
|
|
|
|
|
import { config } from "../config/env";
|
|
|
|
|
import Redis from "ioredis";
|
|
|
|
|
|
|
|
|
|
interface EmailJob {
|
|
|
|
|
to: string
|
|
|
|
|
subject: string
|
|
|
|
|
html: string
|
|
|
|
|
text: string
|
|
|
|
|
to: string;
|
|
|
|
|
subject: string;
|
|
|
|
|
html: string;
|
|
|
|
|
text: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const connection = new Redis(config.redis.url, {
|
|
|
|
|
maxRetriesPerRequest: null
|
|
|
|
|
})
|
|
|
|
|
export const QUEUE_NAME = "{lfkmailqueue}";
|
|
|
|
|
|
|
|
|
|
export const emailQueue = new Queue<EmailJob>('email', {
|
|
|
|
|
const connection = new Redis(config.redis.url, {
|
|
|
|
|
maxRetriesPerRequest: null,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const emailQueue = new Queue<EmailJob>(QUEUE_NAME, {
|
|
|
|
|
connection,
|
|
|
|
|
defaultJobOptions: {
|
|
|
|
|
attempts: 3,
|
|
|
|
|
backoff: {
|
|
|
|
|
type: 'exponential',
|
|
|
|
|
delay: 1000
|
|
|
|
|
}
|
|
|
|
|
type: "exponential",
|
|
|
|
|
delay: 1000,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const transporter = createTransport({
|
|
|
|
|
host: config.smtp.host,
|
|
|
|
|
port: config.smtp.port,
|
|
|
|
|
auth: {
|
|
|
|
|
user: config.smtp.user,
|
|
|
|
|
pass: config.smtp.pass
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const worker = new Worker<EmailJob>(
|
|
|
|
|
'email',
|
|
|
|
|
QUEUE_NAME,
|
|
|
|
|
async (job) => {
|
|
|
|
|
const emailService = new EmailService()
|
|
|
|
|
await emailService.sendEmail(job.data)
|
|
|
|
|
// const emailService = new EmailService();
|
|
|
|
|
// await emailService.sendEmail(job.data);
|
|
|
|
|
console.log(job.data);
|
|
|
|
|
await transporter.sendMail({
|
|
|
|
|
from: config.email.from,
|
|
|
|
|
to: job.data.to,
|
|
|
|
|
subject: job.data.subject,
|
|
|
|
|
text: job.data.text,
|
|
|
|
|
html: job.data.html
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
connection,
|
|
|
|
|
concurrency: 5,
|
|
|
|
|
limiter: {
|
|
|
|
|
max: 100,
|
|
|
|
|
duration: 1000 * 60 // 100 emails per minute
|
|
|
|
|
}
|
|
|
|
|
max: 250,
|
|
|
|
|
duration: 1000 * 60, // 250 emails per minute
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const queueEvents = new QueueEvents('email', { connection })
|
|
|
|
|
const queueEvents = new QueueEvents(QUEUE_NAME, { connection });
|
|
|
|
|
|
|
|
|
|
worker.on('completed', (job) => {
|
|
|
|
|
console.log(`Email job ${job.id} completed`)
|
|
|
|
|
})
|
|
|
|
|
worker.on("completed", (job) => {
|
|
|
|
|
console.log(`Email job ${job.id} completed`);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
worker.on('failed', (job, error) => {
|
|
|
|
|
console.error(`Email job ${job?.id} failed:`, error)
|
|
|
|
|
})
|
|
|
|
|
worker.on("failed", (job, error) => {
|
|
|
|
|
console.error(`Email job ${job?.id} failed:`, error);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
queueEvents.on('waiting', ({ jobId }) => {
|
|
|
|
|
console.log(`Job ${jobId} is waiting`)
|
|
|
|
|
})
|
|
|
|
|
queueEvents.on("waiting", ({ jobId }) => {
|
|
|
|
|
// console.log(`Job ${jobId} is waiting`)
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
process.on('SIGTERM', async () => {
|
|
|
|
|
await worker.close()
|
|
|
|
|
await connection.quit()
|
|
|
|
|
})
|
|
|
|
|
process.on("SIGTERM", async () => {
|
|
|
|
|
await worker.close();
|
|
|
|
|
await connection.quit();
|
|
|
|
|
});
|
|
|
|
|
|