-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
91 lines (82 loc) · 2.56 KB
/
Copy pathworker.js
File metadata and controls
91 lines (82 loc) · 2.56 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import express from "express";
import amqp from "amqplib";
import cluster from "cluster";
import dotenv from "dotenv";
dotenv.config();
if (cluster.isPrimary) {
for (let i = 0; i < process.env.NO_OF_WORKERS; i++) {
cluster.fork({ workerId: i + 1 });
}
} else {
const app = express();
const queueName = "tasks";
const resultQueueName = "results";
const port = 8080;
console.log(`Worker ${cluster.worker.id} started`);
async function connectToRabbitMQ() {
const rabbitMQUrl = "amqp://localhost";
const connection = await amqp.connect(rabbitMQUrl);
const channel = await connection.createChannel();
await channel.assertQueue(queueName);
await channel.assertQueue(resultQueueName);
return channel;
}
async function processTask(task, channel) {
if (task.workerId === cluster.worker.id) {
await simulateProcessing(task);
console.log(`Worker ${cluster.worker.id} is sending result...`);
const result = {
taskId: task.id,
result: "Processing complete",
};
await channel.sendToQueue(
resultQueueName,
Buffer.from(JSON.stringify(result))
);
console.log(
`Task processed by Worker ${cluster.worker.id}. Result sent to Supervisor`
);
} else {
await channel.sendToQueue(queueName, Buffer.from(JSON.stringify(task)));
}
}
async function simulateProcessing(task) {
return new Promise((resolve) => {
setTimeout(() => {
resolve();
}, 10000); // Simulating a 10-second processing time
});
}
async function startWorker() {
try {
console.log(
`Worker ${cluster.worker.id} is listening to the RabbitMQ queue for tasks`
);
const channel = await connectToRabbitMQ();
channel.consume(queueName, async (msg) => {
if (msg !== null) {
const task = JSON.parse(msg.content.toString());
console.log(
`Worker ${cluster.worker.id} is processing task: ${task.id}`
);
await processTask(task, channel);
channel.ack(msg);
}
});
console.log(
`Worker ${cluster.worker.id} is listening to the RabbitMQ queue for tasks`
);
} catch (error) {
console.error(
`Error connecting to RabbitMQ in Worker ${cluster.worker.id}:`,
error.message
);
}
}
startWorker();
app.listen(port, () => {
console.log(
`Worker ${cluster.worker.id} listening at http://localhost:${port}`
);
});
}