@@ -4,7 +4,7 @@ const { v4: uuidv4 } = require('uuid');
4
4
const { addMatchedPair, getCurrentMatchedPair } = require ( '../database/matchedPairDb' ) ;
5
5
const MatchedPair = require ( '../models/matchedPairModel' ) ;
6
6
7
- const refreshDuration = 3000 ; // 5 seconds
7
+ const refreshDuration = 3000 ; // 3 seconds
8
8
const waitingDuration = 3000 ;
9
9
const matchingDuration = 60000 - waitingDuration ;
10
10
const queueName = 'matchingQueue' ;
@@ -18,26 +18,32 @@ async function findMatch(request) {
18
18
let connection ;
19
19
let channel ;
20
20
let checkCancel ;
21
+
21
22
try {
22
23
connection = await amqp . connect ( config . rabbitmqUrl ) ;
23
24
channel = await connection . createChannel ( ) ;
25
+
24
26
console . log ( 'Successfully connected to RabbitMQ' ) ;
25
27
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 } ` ;
27
32
28
33
checkCancel = setInterval ( async ( ) => {
29
34
if ( isCancelled . has ( parseInt ( request . id ) ) ) {
30
- console . log ( `Always checking` ) ;
31
35
clearInterval ( checkCancel ) ;
32
36
resolve ( { isMatched : false , collaboratorId : null , request : request } ) ;
33
37
34
38
} else {
35
39
const checkMatchedPair = await getCurrentMatchedPair ( request . id ) ;
40
+
36
41
if ( checkMatchedPair ) {
37
42
clearInterval ( checkCancel ) ;
38
43
resolve ( {
39
44
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 ) ,
41
47
request : request
42
48
} ) ;
43
49
}
@@ -46,18 +52,25 @@ async function findMatch(request) {
46
52
47
53
await addRequestIntoQueue ( channel , criteria , request ) ;
48
54
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
50
57
51
- const { stored, isMatched, id, collaboratorId } = await getMatchFromQueue ( channel , criteria , request ) ;
58
+ const { stored, isMatched, id, collaboratorId } =
59
+ await getMatchFromQueue ( channel , criteria , request ) ;
52
60
53
61
console . log ( `Clean up tasks are completed for ${ request . id } !` ) ;
54
62
55
63
if ( ! isMatched ) {
56
64
console . log ( `Matched pair could not be found for ${ request . id } ` ) ;
65
+
57
66
resolve ( { isMatched : false , collaboratorId : null , request : request } ) ;
58
67
59
68
} 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
+ } ) ;
61
74
62
75
} else if ( isMatched && ! stored ) {
63
76
const matchedPair = new MatchedPair ( {
@@ -69,17 +82,21 @@ async function findMatch(request) {
69
82
proficiency : request . proficiency ,
70
83
difficulty : request . difficulty ,
71
84
topic : request . topic
72
-
73
85
} ) ;
74
86
75
87
await addMatchedPair ( matchedPair ) ;
76
88
77
- resolve ( { isMatched : true , collaboratorId : parseInt ( collaboratorId ) , request : request } ) ;
89
+ resolve ( {
90
+ isMatched : true ,
91
+ collaboratorId : parseInt ( collaboratorId ) ,
92
+ request : request
93
+ } ) ;
78
94
}
79
95
} catch ( error ) {
80
96
console . log ( 'Error finding match: ' , error ) ;
81
97
} finally {
82
98
availabilityCache . delete ( request . id ) ;
99
+
83
100
if ( channel ) {
84
101
channel . close ( ) ;
85
102
}
@@ -111,10 +128,13 @@ async function addRequestIntoQueue(channel, criteria, request) {
111
128
// Check if there exists a matched pair for the user, else, find a match from the queue
112
129
async function getMatchFromQueue ( channel , criteria , request ) {
113
130
console . log ( `Checking if there is a match for user ${ request . id } and find match from queue...` ) ;
131
+
114
132
const currentPair = await getCurrentMatchedPair ( request . id ) ;
115
133
116
134
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
+
118
138
return { stored : true , isMatched : true , id : request . id , collaboratorId : collaboratorId } ;
119
139
120
140
} else {
@@ -126,13 +146,19 @@ async function getMatchFromQueue(channel, criteria, request) {
126
146
async function listenToMatchingQueue ( channel , criteria , request ) {
127
147
try {
128
148
console . log ( `Start matching user ${ request . id } ` ) ;
149
+
129
150
await channel . assertQueue ( queueName , { durable : false } ) ;
130
151
131
152
let matched = false ;
132
153
return new Promise ( async ( resolve ) => {
133
154
setTimeout ( ( ) => {
134
155
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
+ } ) ;
136
162
}
137
163
} , matchingDuration ) ;
138
164
@@ -143,27 +169,46 @@ async function listenToMatchingQueue(channel, criteria, request) {
143
169
const checkActivePair = await getCurrentMatchedPair ( currentRequest . request . id ) ;
144
170
145
171
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 ) ) ) {
148
184
149
- } else if ( checkActivePair || isCancelled . has ( parseInt ( currentRequest . request . id ) ) ) {
150
185
console . log ( `Remove match ${ currentRequest . request . id } ` ) ;
186
+
151
187
availabilityCache . delete ( currentRequest . request . id ) ;
152
188
channel . ack ( message ) ;
153
189
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
+
156
195
console . log ( `Found a match for ${ request . id } ` ) ;
157
196
158
197
channel . ack ( message ) ;
159
198
availabilityCache . delete ( currentRequest . request . id ) ;
160
199
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 } ` ) ;
162
202
163
203
matched = true ;
164
204
console . log ( `Successfully matched user ${ request . id } ` ) ;
165
205
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
+ } ) ;
167
212
}
168
213
} ) ;
169
214
} ) ;
@@ -177,7 +222,9 @@ async function listenToMatchingQueue(channel, criteria, request) {
177
222
async function cancelMatch ( requestId ) {
178
223
isCancelled . add ( parseInt ( requestId ) ) ;
179
224
availabilityCache . delete ( requestId ) ;
225
+
180
226
console . log ( `Matching service is cancelled for ${ requestId } ` ) ;
227
+
181
228
return true ;
182
229
}
183
230
0 commit comments