Skip to content

Commit 17b6fb6

Browse files
committed
add background queues to handle heavy task
1 parent 96481a5 commit 17b6fb6

File tree

5 files changed

+97
-2
lines changed

5 files changed

+97
-2
lines changed

backend/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ RUN npm run build
1515
EXPOSE 3000
1616

1717
# Start Redis + Node
18-
CMD redis-server --daemonize yes && node ./dist/server.js
18+
CMD redis-server --daemonize yes && npm run start2

backend/package-lock.json

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
"test": "jest",
88
"client": " npm run dev --prefix ../frontend",
99
"start": "node ./dist/server.js",
10+
"worker": "node ./dist/worker.js",
1011
"dev": "nodemon",
12+
"start2": "concurrently \" npm start \" \" npm run worker \"",
1113
"build": "tsc"
1214
},
1315
"author": "Aryan Gawade (NoB0T)",
@@ -23,6 +25,7 @@
2325
"@types/jsonwebtoken": "^9.0.10",
2426
"@types/multer": "^1.4.13",
2527
"@types/node": "^24.0.3",
28+
"@types/nodemailer": "^7.0.9",
2629
"@types/uuid4": "^2.0.3",
2730
"concurrently": "^9.1.2",
2831
"dotenv-cli": "^8.0.0",
@@ -47,6 +50,7 @@
4750
"jsonwebtoken": "^9.0.2",
4851
"mongoose": "^8.16.0",
4952
"multer": "^2.0.1",
53+
"nodemailer": "^8.0.0",
5054
"uuid4": "^2.0.3",
5155
"ws": "^8.18.2"
5256
}

backend/src/controller/user.controller.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import userModel from '../Models/user.model'
55
import jwt from 'jsonwebtoken'
66
import uuid4 from "uuid4"
77
import supabase from "../Db/supabase"
8+
import redis from "../Db/redis";
89

910
const client = new OAuth2Client(process.env.GOOGLE_ID)
1011

@@ -27,6 +28,13 @@ export const register = async (request: Request, response: any) => {
2728
try {
2829
const existingUsers = await findUser({email})
2930
if(existingUsers){
31+
const job = {
32+
type: "sendEmail",
33+
to: email,
34+
subject: 'Wellcome back',
35+
};
36+
console.log('job added')
37+
await redis.lpush("jobs", JSON.stringify(job))
3038
return response.status(202).json({
3139
message: "email already exists, Please Sign-in",
3240
user: existingUsers,
@@ -95,7 +103,6 @@ export const register = async (request: Request, response: any) => {
95103
}
96104

97105
export const login = async (request: Request, response:any) => {
98-
console.log(request.body)
99106
const {email, password} = request.body
100107
if(!email||!password){
101108
return response.status(400).json({
@@ -118,6 +125,12 @@ export const login = async (request: Request, response:any) => {
118125
success: false,
119126
})}
120127
const token = await user.generateToken()
128+
const job = {
129+
type: "sendEmail",
130+
to: email,
131+
subject: 'Wellcome back',
132+
};
133+
await redis.lpush("jobs", JSON.stringify(job));
121134
return response.status(201).json({
122135
message: "User login successfully",
123136
user,

backend/src/worker.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import Redis from "ioredis";
2+
import nodemailer from "nodemailer";
3+
import dotenv from "dotenv"
4+
dotenv.config();
5+
6+
const transporter = nodemailer.createTransport({
7+
host: "smtp.gmail.com",
8+
port: 465,
9+
secure: true,
10+
auth: {
11+
user: process.env.EMAIL_USER,
12+
pass: process.env.EMAIL_PASS,
13+
},
14+
});
15+
const redis = new Redis(process.env.REDIS_URL||'');
16+
17+
async function sendEmail(to: string) {
18+
await transporter.sendMail({
19+
from: process.env.EMAIL_USER,
20+
to,
21+
subject: "Welcome back",
22+
text: "Hello Aryan 👋",
23+
});
24+
}
25+
26+
async function startWorker() {
27+
console.log("Worker started...");
28+
29+
while (true) {
30+
const result = await redis.brpop("jobs", 0);
31+
32+
if (!result) continue;
33+
34+
const [, jobString] = result;
35+
const job = JSON.parse(jobString);
36+
37+
await processJob(job);
38+
}
39+
}
40+
41+
async function processJob(job: any) {
42+
console.log("Processing:", job);
43+
console.log(process.env.EMAIL_PASS)
44+
console.log(process.env.EMAIL_USER)
45+
try {
46+
console.log("Sending email...");
47+
48+
await sendEmail(job.to);
49+
50+
console.log("Email sent successfully!");
51+
} catch (err) {
52+
console.error("Email failed:", err);
53+
}
54+
await new Promise(r => setTimeout(r, 2000));
55+
}
56+
57+
startWorker();

0 commit comments

Comments
 (0)