-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (51 loc) · 1.25 KB
/
index.js
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
const Queue = require("bull");
const nodemailer = require("nodemailer"); // 1. Initiating the Queue
try {
const sendMailQueue = new Queue("sendMail", {
redis: {
host: "127.0.0.1",
port: 6379,
},
});
const data = {
email: "[email protected]",
};
const options = {
delay: 60000, // 1 min in ms
attempts: 2,
}; // 2. Adding a Job to the Queue
sendMailQueue.add(data, options);
// 3. Consumer
sendMailQueue.process(async (job) => {
return await sendMail(job.data.email);
});
function sendMail(email) {
return new Promise((resolve, reject) => {
let mailOptions = {
from: "[email protected]",
to: email,
subject: "Bull - npm",
text: "This email is from bull job scheduler tutorial.",
};
let mailConfig = {
service: "gmail",
auth: {
user: "[email protected]",
pass: "techtig0605##",
},
};
nodemailer
.createTransport(mailConfig)
.sendMail(mailOptions, (err, info) => {
if (err) {
reject(err);
} else {
console.log(info);
resolve(info);
}
});
});
}
} catch (error) {
console.log(error);
}