Skip to content

Commit c6234a1

Browse files
committed
better set up for background jobs
1 parent 46e7710 commit c6234a1

6 files changed

Lines changed: 80 additions & 48 deletions

File tree

‎backend-express/src/app.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { router as routerUsers } from "./module/user/router";
1111
import { appMetric } from "./middleware/app-metric.middleware";
1212
import { rateLimit } from "./utils/config/rate-limiter.config";
1313
import { router as routerPractice } from "./module/practice/router";
14-
import { dashboardPath, runBullBoard, serverAdapter } from "./utils/config/worker-dashboard.config";
14+
import { dashboardPath, runBullBoard, serverAdapter } from "./utils/background-job/dashboard";
1515

1616
const app: Express = express()
1717

‎backend-express/src/module/practice/controller.ts‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,36 @@ import { User, userModel } from "../user/model";
33
import { errorHandler } from "../../utils/error-handler";
44
import mongoose, { model } from "mongoose";
55
import { clientRedis } from "../../utils/config/redis.config";
6-
import { queueService } from "../../utils/config/worker.config ";
6+
import { queueManager } from "../../utils/background-job/index";
7+
// import { queueService } from "../../utils/config/worker.config ";
78

8-
const queue = queueService?.queue
9+
const emailQueue = queueManager?.get('email')
10+
const notificationQueue = queueManager?.get('notification')
911

1012
export const practiceWorker = async (
1113
req: Request, res: Response
1214
) => {
1315
try {
14-
if (!queue) {
16+
if (!emailQueue) {
1517
return res.status(503).json({
1618
message: "Queue is down (Redis not connected)",
1719
});
1820
}
1921

20-
await queue.add("test-job", {
21-
name: "pritam debnath",
22+
await emailQueue.add("test-job", {
2223
action: "send-email",
2324
});
2425

25-
await queue.add("test-job", {
26+
await emailQueue.add("test-job", {
2627
name: "fail",
27-
fail: true,
2828
});
2929

30+
setTimeout(async () => {
31+
await notificationQueue?.add("sent-after-2-scond", {
32+
name: "anything",
33+
});
34+
}, 2000);
35+
3036
return res.json({
3137
success: true,
3238
message: "Jobs added to queue",

backend-express/src/utils/config/worker-dashboard.config.ts renamed to backend-express/src/utils/background-job/dashboard.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import { createBullBoard } from "@bull-board/api";
22
import { BullMQAdapter } from "@bull-board/api/bullMQAdapter";
33
import { ExpressAdapter } from "@bull-board/express";
4-
import { queueService } from "./worker.config ";
4+
import { queueManager } from "../background-job/index";
55

66
export const dashboardPath = "/queues";
77

88
export const serverAdapter = new ExpressAdapter();
99
serverAdapter.setBasePath(dashboardPath);
1010

1111
export function runBullBoard() {
12-
if (!queueService) return;
13-
12+
if (!queueManager) return;
1413
createBullBoard({
15-
queues: [new BullMQAdapter(queueService.queue)],
14+
queues: queueManager
15+
.getAll()
16+
.map(queue => new BullMQAdapter(queue)),
1617
serverAdapter,
1718
});
1819
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { env } from "../load-env";
2+
import { runBullBoard } from "./dashboard";
3+
import { QueueManager } from "./manager"
4+
5+
export const QUEUES = {
6+
"email": "email",
7+
notification: "notification",
8+
} as const
9+
10+
export const connection = {
11+
url: env.REDIS_URL
12+
}
13+
14+
export const queueManager = connection?.url ? new QueueManager() : null;
15+
16+
// register queues before running dashboard
17+
queueManager?.register(QUEUES.email, connection)
18+
queueManager?.register(QUEUES.notification, connection)
19+
20+
if (queueManager) {
21+
console.log("🟩 Queue running");
22+
runBullBoard();
23+
} else {
24+
console.log("🟨 Redis disabled thus background jobs are disabled");
25+
}
26+
27+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Queue, Worker, Processor, type ConnectionOptions } from "bullmq";
2+
import { QUEUES} from "./index"
3+
4+
type QUEUES_NAME = keyof typeof QUEUES
5+
6+
export class QueueManager {
7+
private readonly queues = new Map<QUEUES_NAME, Queue>();
8+
9+
register(name: QUEUES_NAME, connection: ConnectionOptions) {
10+
const queue = new Queue(
11+
name,
12+
{ connection }
13+
);
14+
15+
new Worker(
16+
name,
17+
async (job) => {
18+
console.log("Processing:", job.data);
19+
},
20+
{ connection }
21+
);
22+
23+
this.queues.set(name, queue);
24+
}
25+
26+
get(name: QUEUES_NAME) {
27+
return this.queues.get(name);
28+
}
29+
30+
getAll() {
31+
return [...this.queues.values()];
32+
}
33+
}
34+

‎backend-express/src/utils/config/worker.config .ts‎

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)