Skip to content

Commit c185cad

Browse files
committed
Add dependency on QuestionService to MatchingService
1 parent 3b9f5e8 commit c185cad

File tree

6 files changed

+29
-13
lines changed

6 files changed

+29
-13
lines changed

Backend/MatchingService/rabbitmq/setup.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const dead_letter_queue_name = "dead_letter_queue";
77
// const cancel_queue_name = "cancel_queue";
88
const difficulties = ["easy", "medium", "hard", "any"];
99
const axios = require('axios');
10-
const categoryAPIUrl = 'http://localhost:3001/api/categories';
10+
const categoryAPIUrl = 'http://question-service:3001/api/categories';
1111

1212
let queueNamesPromise = axios.get(categoryAPIUrl)
1313
.then(response => {

Backend/MatchingService/rabbitmq/subscriber.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const { notifyUsers } = require('../websocket/websocket');
55
const { v4: uuidv4 } = require('uuid');
66
// matchingService/fetchQuestion.js
77
const axios = require('axios');
8-
const questionAPIUrl = 'http://localhost:3001/api/questions';
8+
const questionAPIUrl = 'http://question-service:3001/api/questions';
99

1010

1111
// TODO: Subscribe and acknowledge messages with user info when timeout/user matched
@@ -77,15 +77,19 @@ function matchUsers(channel, msg, userId, difficulty, category) {
7777

7878
// Try matching with category only if no difficulty match is found
7979
if (waitingUsers[criteriaKey].length < 2) {
80-
waitingUsers[categoryKey].push({ userId, msg, channel });
81-
console.log(`Fallback: User ${userId} added to ${categoryKey}. Waiting list: ${waitingUsers[categoryKey].length}`);
82-
if (waitingUsers[categoryKey].length >= 2) {
83-
const matchedUsers = waitingUsers[categoryKey].splice(0, 2);
84-
removeMatchedUsersFromOtherLists(matchedUsers, categoryKey);
85-
console.log("waitingusers after lenient matching: ", waitingUsers)
86-
notifyMatch(channel, matchedUsers, category, difficulty);
87-
return true;
88-
}
80+
// Add a 5-second delay before adding the user to waitingUsers[categoryKey]
81+
setTimeout(() => {
82+
waitingUsers[categoryKey].push({ userId, msg, channel });
83+
console.log(`Fallback: User ${userId} added to ${categoryKey}. Waiting list: ${waitingUsers[categoryKey].length}`);
84+
85+
if (waitingUsers[categoryKey].length >= 2) {
86+
const matchedUsers = waitingUsers[categoryKey].splice(0, 2);
87+
removeMatchedUsersFromOtherLists(matchedUsers, categoryKey);
88+
console.log("waitingusers after lenient matching: ", waitingUsers)
89+
notifyMatch(channel, matchedUsers, category, difficulty);
90+
return true;
91+
}
92+
}, 5000); // 5-second delay
8993
}
9094

9195
return false;

Backend/QuestionService/app.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ app.get("/", (req, res, next) => {
2626
});
2727
});
2828

29+
app.get('/health', (req, res) => {
30+
res.status(200).json({ status: 'healthy' });
31+
});
32+
2933
// Handle When No Route Match Is Found
3034
app.use((req, res, next) => {
3135
const error = new Error("Route Not Found");

Frontend/src/services/categories.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import axios from 'axios';
22

3-
const baseCategoryUrl = 'http://localhost:3001/api/categories';
3+
const baseCategoryUrl = 'http://question-service:3001/api/categories';
44

55
export const getAllCategories = async () => {
66
console.log("getAllCategories func called")

Frontend/src/services/questions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import axios from 'axios'
22

3-
const baseUrl = 'http://localhost:3001/api/questions'
3+
const baseUrl = 'http://question-service:3001/api/questions'
44

55
const getAll = () => {
66
const request = axios.get(baseUrl);

docker-compose.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ services:
55
dockerfile: Dockerfile
66
ports:
77
- "3001:3001"
8+
healthcheck:
9+
test: ["CMD", "curl", "-f", "http://question-service:3001/health"]
10+
interval: 10s
11+
timeout: 5s
12+
retries: 5
813

914
user-service:
1015
build:
@@ -19,6 +24,9 @@ services:
1924
dockerfile: Dockerfile
2025
ports:
2126
- "8080:8080"
27+
depends_on:
28+
question-service:
29+
condition: service_healthy
2230

2331
collab-service:
2432
build:

0 commit comments

Comments
 (0)