1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
import { Queue, Worker } from "bullmq";
import { connection } from "../db/redis/info";
import { getTransporter } from "../email/email";
import { verificationEmailHtml } from "../email/confirm";
import { logger } from "../lib/pino";
/** Queue for handling email verification messages */
export const verificationEmailQueue = new Queue("verificationEmailQueue", {
connection,
});
/** Worker that processes verification email jobs from the queue */
export const verificationEmailWorker = new Worker(
"verificationEmailQueue",
async (job) => {
const { email, code } = job.data;
logger.info({ jobId: job.id, email }, "Processing verification email job");
try {
await getTransporter().sendMail({
from: `"Buddy 🐶" <${process.env.SMTP_EMAIL}>`,
to: email,
subject: "Buddy email verification",
html: verificationEmailHtml(code),
});
logger.info(
{ jobId: job.id, email },
"Verification email sent successfully",
);
return { success: true };
} catch (error) {
logger.error(
{ error, jobId: job.id, email },
"Failed to send verification email",
);
throw error;
}
},
{
connection,
concurrency: 5,
},
);
verificationEmailWorker.on("active", (job) => {
logger.debug(
{ jobId: job!.id, email: job!.data.email },
"Email job is active",
);
});
verificationEmailWorker.on("completed", (job) => {
logger.info(
{ jobId: job!.id, email: job!.data.email },
"Email job completed",
);
});
verificationEmailWorker.on("error", (err) => {
logger.error({ error: err }, "Email worker error");
});
verificationEmailWorker.on("failed", (job, err) => {
logger.error(
{ error: err, jobId: job?.id, email: job?.data.email },
"Email job failed",
);
});
|