Skip to content

Commit dc80d5e

Browse files
committed
Fix bugs.
1 parent fb86c90 commit dc80d5e

File tree

1 file changed

+65
-18
lines changed

1 file changed

+65
-18
lines changed

peer-prep/src/matching-service/services/matchingService.js

Lines changed: 65 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { v4: uuidv4 } = require('uuid');
44
const { addMatchedPair, getCurrentMatchedPair } = require('../database/matchedPairDb');
55
const MatchedPair = require('../models/matchedPairModel');
66

7-
const refreshDuration = 3000; // 5 seconds
7+
const refreshDuration = 3000; // 3 seconds
88
const waitingDuration = 3000;
99
const matchingDuration = 60000 - waitingDuration;
1010
const queueName = 'matchingQueue';
@@ -18,26 +18,32 @@ async function findMatch(request) {
1818
let connection;
1919
let channel;
2020
let checkCancel;
21+
2122
try {
2223
connection = await amqp.connect(config.rabbitmqUrl);
2324
channel = await connection.createChannel();
25+
2426
console.log('Successfully connected to RabbitMQ');
2527

26-
const criteria = `${request.language}.${request.proficiency}.${request.difficulty}.${request.topic}`;
28+
const criteria = `${request.language}
29+
.${request.proficiency}
30+
.${request.difficulty}
31+
.${request.topic}`;
2732

2833
checkCancel = setInterval(async() => {
2934
if (isCancelled.has(parseInt(request.id))) {
30-
console.log(`Always checking`);
3135
clearInterval(checkCancel);
3236
resolve({ isMatched: false, collaboratorId: null, request: request });
3337

3438
} else {
3539
const checkMatchedPair = await getCurrentMatchedPair(request.id);
40+
3641
if (checkMatchedPair) {
3742
clearInterval(checkCancel);
3843
resolve({
3944
isMatched: true,
40-
collaboratorId: String(checkMatchedPair.id1) === String(request.id) ? parseInt(checkMatchedPair.id2) : parseInt(checkMatchedPair.id1),
45+
collaboratorId: String(checkMatchedPair.id1) === String(request.id) ?
46+
parseInt(checkMatchedPair.id2) : parseInt(checkMatchedPair.id1),
4147
request: request
4248
});
4349
}
@@ -46,18 +52,25 @@ async function findMatch(request) {
4652

4753
await addRequestIntoQueue(channel, criteria, request);
4854

49-
await new Promise(resolve => setTimeout(resolve, 5000)); // wait for 5 seconds to check if there is a prior object that matched
55+
await new Promise(resolve => setTimeout(resolve, waitingDuration));
56+
// wait for 5 seconds to check if there is a prior object that matched
5057

51-
const { stored, isMatched, id, collaboratorId } = await getMatchFromQueue(channel, criteria, request);
58+
const { stored, isMatched, id, collaboratorId } =
59+
await getMatchFromQueue(channel, criteria, request);
5260

5361
console.log(`Clean up tasks are completed for ${request.id}!`);
5462

5563
if (!isMatched) {
5664
console.log(`Matched pair could not be found for ${request.id}`);
65+
5766
resolve({ isMatched: false, collaboratorId: null, request: request });
5867

5968
} else if (stored) {
60-
resolve({ isMatched: true, collaboratorId: parseInt(collaboratorId), request: request });
69+
resolve({
70+
isMatched: true,
71+
collaboratorId: parseInt(collaboratorId),
72+
request: request
73+
});
6174

6275
} else if (isMatched && !stored) {
6376
const matchedPair = new MatchedPair({
@@ -69,17 +82,21 @@ async function findMatch(request) {
6982
proficiency: request.proficiency,
7083
difficulty: request.difficulty,
7184
topic: request.topic
72-
7385
});
7486

7587
await addMatchedPair(matchedPair);
7688

77-
resolve({ isMatched: true, collaboratorId: parseInt(collaboratorId), request: request });
89+
resolve({
90+
isMatched: true,
91+
collaboratorId: parseInt(collaboratorId),
92+
request: request
93+
});
7894
}
7995
} catch (error) {
8096
console.log('Error finding match: ', error);
8197
} finally {
8298
availabilityCache.delete(request.id);
99+
83100
if (channel) {
84101
channel.close();
85102
}
@@ -111,10 +128,13 @@ async function addRequestIntoQueue(channel, criteria, request) {
111128
// Check if there exists a matched pair for the user, else, find a match from the queue
112129
async function getMatchFromQueue(channel, criteria, request) {
113130
console.log(`Checking if there is a match for user ${request.id} and find match from queue...`);
131+
114132
const currentPair = await getCurrentMatchedPair(request.id);
115133

116134
if (currentPair) {
117-
const collaboratorId = String(currentPair.id1) === String(request.id) ? currentPair.id2 : currentPair.id1;
135+
const collaboratorId =
136+
String(currentPair.id1) === String(request.id) ? currentPair.id2 : currentPair.id1;
137+
118138
return { stored: true, isMatched: true, id: request.id, collaboratorId: collaboratorId };
119139

120140
} else {
@@ -126,13 +146,19 @@ async function getMatchFromQueue(channel, criteria, request) {
126146
async function listenToMatchingQueue(channel, criteria, request) {
127147
try {
128148
console.log(`Start matching user ${request.id}`);
149+
129150
await channel.assertQueue(queueName, { durable: false });
130151

131152
let matched = false;
132153
return new Promise(async(resolve) => {
133154
setTimeout(() => {
134155
if (!matched) {
135-
resolve({ stored: false, isMatched: false, id: request.id, collaboratorId: null });
156+
resolve({
157+
stored: false,
158+
isMatched: false,
159+
id: request.id,
160+
collaboratorId: null
161+
});
136162
}
137163
}, matchingDuration);
138164

@@ -143,27 +169,46 @@ async function listenToMatchingQueue(channel, criteria, request) {
143169
const checkActivePair = await getCurrentMatchedPair(currentRequest.request.id);
144170

145171
if (checkActivePair &&
146-
(String(checkActivePair.id1) === String(request.id) || String(checkActivePair.id2) === String(request.id))) {
147-
resolve({ stored: true, isMatched: true, id: request.id, collaboratorId: currentRequest.request.id });
172+
(String(checkActivePair.id1) === String(request.id) ||
173+
String(checkActivePair.id2) === String(request.id))) {
174+
175+
resolve({
176+
stored: true,
177+
isMatched: true,
178+
id: request.id,
179+
collaboratorId: currentRequest.request.id
180+
});
181+
182+
} else if (checkActivePair ||
183+
isCancelled.has(parseInt(currentRequest.request.id))) {
148184

149-
} else if (checkActivePair || isCancelled.has(parseInt(currentRequest.request.id))) {
150185
console.log(`Remove match ${currentRequest.request.id}`);
186+
151187
availabilityCache.delete(currentRequest.request.id);
152188
channel.ack(message);
153189

154-
} else if (!matched && currentRequest.request.id !== request.id &&
155-
currentRequest.criteria === criteria && availabilityCache.has(currentRequest.request.id)) {
190+
} else if (!matched &&
191+
currentRequest.request.id !== request.id &&
192+
currentRequest.criteria === criteria &&
193+
availabilityCache.has(currentRequest.request.id)) {
194+
156195
console.log(`Found a match for ${request.id}`);
157196

158197
channel.ack(message);
159198
availabilityCache.delete(currentRequest.request.id);
160199
availabilityCache.delete(request.id);
161-
console.log(`${request.id} has been matched with ${currentRequest.request.id}`);
200+
console.log(`${request.id} has been matched with
201+
${currentRequest.request.id}`);
162202

163203
matched = true;
164204
console.log(`Successfully matched user ${request.id}`);
165205

166-
resolve({ stored: false, isMatched: matched, id: request.id, collaboratorId: currentRequest.request.id });
206+
resolve({
207+
stored: false,
208+
isMatched: matched,
209+
id: request.id,
210+
collaboratorId: currentRequest.request.id
211+
});
167212
}
168213
});
169214
});
@@ -177,7 +222,9 @@ async function listenToMatchingQueue(channel, criteria, request) {
177222
async function cancelMatch(requestId) {
178223
isCancelled.add(parseInt(requestId));
179224
availabilityCache.delete(requestId);
225+
180226
console.log(`Matching service is cancelled for ${requestId}`);
227+
181228
return true;
182229
}
183230

0 commit comments

Comments
 (0)