Skip to content

Commit f917b19

Browse files
committed
Merge branch 'main' into hist
2 parents 575d8b9 + fbeb72e commit f917b19

29 files changed

+1271
-421
lines changed

Backend/MatchingService/package-lock.json

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

Backend/MatchingService/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"dependencies": {
33
"amqplib": "^0.10.4",
4+
"axios": "^1.7.7",
45
"cors": "^2.8.5",
56
"dotenv": "^16.4.5",
67
"express": "^4.21.1",
Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const amqp = require('amqplib');
22
const { matching_exchange_name } = require('./setup.js');
33

4-
let channel = null; // Store a persistent channel connection
4+
let channel = null;
55

66
async function connectToRabbitMQ() {
77
if (!channel) {
@@ -16,16 +16,15 @@ async function connectToRabbitMQ() {
1616
return channel;
1717
}
1818

19-
async function publishToQueue({userId, difficulty, language}) {
19+
async function publishToQueue({ userId, difficulty, category }) {
2020
try {
21-
const channel = await connectToRabbitMQ(); // Reuse persistent connection
22-
const routingKey = `${difficulty}.${language}`;
21+
const channel = await connectToRabbitMQ();
22+
const routingKey = `${difficulty}.${category}`;
2323

24-
// Publish the message to the exchange
2524
const messageSent = channel.publish(
2625
matching_exchange_name,
2726
routingKey,
28-
Buffer.from(JSON.stringify({ userId, difficulty, language }))
27+
Buffer.from(JSON.stringify({ userId, difficulty, category }))
2928
);
3029

3130
if (messageSent) {
@@ -38,26 +37,26 @@ async function publishToQueue({userId, difficulty, language}) {
3837
}
3938
}
4039

41-
async function publishCancelRequest({ userId }) {
42-
try {
43-
const channel = await connectToRabbitMQ(); // Reuse persistent connection
44-
const routingKey = 'cancel'; // Define a routing key for cancellation
45-
46-
// Publish the cancel message to the exchange
47-
const messageSent = channel.publish(
48-
matching_exchange_name,
49-
routingKey,
50-
Buffer.from(JSON.stringify({ userId }))
51-
);
52-
53-
if (messageSent) {
54-
console.log(`Cancel request sent: ${userId}`);
55-
} else {
56-
console.error(`Cancel request NOT sent: ${userId}`);
57-
}
58-
} catch (error) {
59-
console.error('Error publishing cancel request to RabbitMQ:', error);
60-
}
61-
}
62-
63-
module.exports = { publishToQueue, publishCancelRequest };
40+
// async function publishCancelRequest({ userId }) {
41+
// try {
42+
// const channel = await connectToRabbitMQ();
43+
// const routingKey = 'cancel';
44+
45+
// const messageSent = channel.publish(
46+
// matching_exchange_name,
47+
// routingKey,
48+
// Buffer.from(JSON.stringify({ userId }))
49+
// );
50+
51+
// if (messageSent) {
52+
// console.log(`Cancel request sent: ${userId}`);
53+
// } else {
54+
// console.error(`Cancel request NOT sent: ${userId}`);
55+
// }
56+
// } catch (error) {
57+
// console.error('Error publishing cancel request to RabbitMQ:', error);
58+
// }
59+
// }
60+
61+
// module.exports = { publishToQueue, publishCancelRequest };
62+
module.exports = { publishToQueue };

Backend/MatchingService/rabbitmq/setup.js

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,27 @@ const generator = require("../utils/generateQueues");
44
const matching_exchange_name = "matching_exchange";
55
const dead_letter_exchange_name = "dead_letter_exchange";
66
const dead_letter_queue_name = "dead_letter_queue";
7-
const cancel_queue_name = "cancel_queue";
8-
const difficulties = ["easy", "medium", "hard"];
9-
const languages = ["python", "java", "cplusplus"];
10-
11-
const queueNames = generator.generateQueueNames(
12-
generator.generateCombinations(difficulties, languages)
13-
);
7+
// const cancel_queue_name = "cancel_queue";
8+
const difficulties = ["easy", "medium", "hard", "any"];
9+
const axios = require('axios');
10+
// Matching Service code
11+
const questionServiceHost = process.env.QUESTION_SERVICE_HOST || 'localhost';
12+
const categoryAPIUrl = `http://${questionServiceHost}:3001/api/categories`;
13+
14+
let queueNamesPromise = axios.get(categoryAPIUrl)
15+
.then(response => {
16+
const categories = response.data.map(category => category.name.toLowerCase().replace(/\s+/g, '-'));
17+
console.log("categories from api: ", categories)
18+
return generator.generateQueueNames(generator.generateCombinations(difficulties, categories));
19+
})
20+
.catch(error => {
21+
console.error("Error fetching categories:", error);
22+
return []; // Return an empty array if categories couldn't be fetched
23+
});
1424

1525
async function setupRabbitMQ() {
1626
try {
27+
const queueNames = await queueNamesPromise;
1728
const connection = await amqp.connect(process.env.RABBITMQ_URL);
1829

1930
if (!connection) {
@@ -30,30 +41,28 @@ async function setupRabbitMQ() {
3041

3142
// Declare and bind all main queues with TTL and DLQ bindings
3243
for (let queueName of queueNames) {
33-
await channel.deleteQueue(queueName); // Ensure we start fresh for each setup
44+
await channel.deleteQueue(queueName); // Ensure we start fresh for each setup
3445

3546
await channel.assertQueue(queueName, {
3647
durable: true,
3748
arguments: {
38-
'x-message-ttl': 10000, // 60 seconds TTL
39-
'x-dead-letter-exchange': dead_letter_exchange_name // Bind to dead-letter exchange
49+
'x-message-ttl': 10000, // TTL for messages in the queue
50+
'x-dead-letter-exchange': dead_letter_exchange_name // Bind to dead-letter exchange
4051
}
4152
});
4253

4354
await channel.bindQueue(queueName, matching_exchange_name, queueName); // Bind to exchange
4455
}
4556

46-
// Delete DLQ before asserting it
57+
// Delete and recreate the DLQ
4758
await channel.deleteQueue(dead_letter_queue_name);
48-
49-
// Declare the dead-letter queue and bind it to the dead-letter exchange
5059
await channel.assertQueue(dead_letter_queue_name, { durable: true });
5160
await channel.bindQueue(dead_letter_queue_name, dead_letter_exchange_name, ''); // Bind with no routing key
5261

53-
// Declare and bind the cancel queue
54-
await channel.deleteQueue(cancel_queue_name); // Delete any existing cancel queue
55-
await channel.assertQueue(cancel_queue_name, { durable: true }); // Declare the cancel queue
56-
await channel.bindQueue(cancel_queue_name, matching_exchange_name, 'cancel'); // Bind with the "cancel" routing key
62+
// // Delete and recreate the cancel queue
63+
// await channel.deleteQueue(cancel_queue_name);
64+
// await channel.assertQueue(cancel_queue_name, { durable: true });
65+
// await channel.bindQueue(cancel_queue_name, matching_exchange_name, 'cancel'); // Bind with the "cancel" routing key
5766

5867
console.log("RabbitMQ setup complete with queues, DLQ, and bindings.");
5968

@@ -64,4 +73,5 @@ async function setupRabbitMQ() {
6473
}
6574
}
6675

67-
module.exports = { setupRabbitMQ, matching_exchange_name, queueNames, dead_letter_queue_name , cancel_queue_name};
76+
// module.exports = { setupRabbitMQ, matching_exchange_name, queueNamesPromise, dead_letter_queue_name, cancel_queue_name };
77+
module.exports = { setupRabbitMQ, matching_exchange_name, queueNamesPromise, dead_letter_queue_name };

0 commit comments

Comments
 (0)