Skip to content

Commit 3518f5b

Browse files
committed
Fix timeout and add logging
1 parent 49f57f6 commit 3518f5b

File tree

3 files changed

+43
-22
lines changed

3 files changed

+43
-22
lines changed

frontend/components/matching/find-match.tsx

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ export default function FindMatch() {
1717
const { toast } = useToast();
1818
const auth = useAuth();
1919

20+
const waitTimeout = 60000;
21+
2022
useEffect(() => {
2123
let interval: NodeJS.Timeout | undefined;
2224
if (isSearching) {
@@ -83,8 +85,8 @@ export default function FindMatch() {
8385
selectedDifficulty
8486
);
8587
const queueTimeout = setTimeout(() => {
86-
handleCancel();
87-
}, 60000);
88+
handleCancel(true);
89+
}, waitTimeout);
8890
ws.onmessage = () => {
8991
setIsSearching(false);
9092
clearTimeout(queueTimeout);
@@ -115,7 +117,7 @@ export default function FindMatch() {
115117
}
116118
};
117119

118-
const handleCancel = async () => {
120+
const handleCancel = async (timedOut: boolean) => {
119121
if (!selectedDifficulty || !selectedTopic) {
120122
toast({
121123
title: "Invalid Selection",
@@ -153,11 +155,19 @@ export default function FindMatch() {
153155
case 200:
154156
setIsSearching(false);
155157
setWaitTime(0);
156-
toast({
157-
title: "Success",
158-
description: "Successfully left queue",
159-
variant: "success",
160-
});
158+
if (timedOut) {
159+
toast({
160+
title: "Timed Out",
161+
description: "Matching has been stopped",
162+
variant: "destructive",
163+
});
164+
} else {
165+
toast({
166+
title: "Matching Stopped",
167+
description: "Matching has been stopped",
168+
variant: "destructive",
169+
});
170+
}
161171
return;
162172
default:
163173
toast({
@@ -178,7 +188,7 @@ export default function FindMatch() {
178188
setSelectedTopic={setSelectedTopic}
179189
handleSearch={handleSearch}
180190
isSearching={isSearching}
181-
handleCancel={handleCancel}
191+
handleCancel={() => handleCancel(false)}
182192
/>
183193

184194
{isSearching && <SearchProgress waitTime={waitTime} />}

matching-service/app/logger.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import logging
2+
import sys
3+
4+
logger = logging.getLogger(__name__)
5+
logger.setLevel(logging.DEBUG)
6+
stream_handler = logging.StreamHandler(sys.stdout)
7+
log_formatter = logging.Formatter("%(levelname)s: %(message)s")
8+
stream_handler.setFormatter(log_formatter)
9+
logger.addHandler(stream_handler)

matching-service/app/logic/matching.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from fastapi.responses import JSONResponse, Response
22
from typing import Union
33

4+
from logger import logger
45
from models.match import MatchModel, MessageModel
56
from utils.redis import acquire_lock, redis_client, release_lock
67
from utils.socketmanager import manager
@@ -17,7 +18,7 @@ async def find_match_else_enqueue(
1718
raise Exception("Could not acquire lock")
1819

1920
queue = await redis_client.lrange(queue_key, 0, -1)
20-
_print_queue_state(topic, difficulty, queue, True)
21+
logger.debug(_get_queue_state_message(topic, difficulty, queue, True))
2122

2223
# Check if the user is already in the queue
2324
if user_id in queue:
@@ -27,17 +28,17 @@ async def find_match_else_enqueue(
2728
# Check if there are no other users in the queue
2829
if await redis_client.llen(queue_key) == 0:
2930
await redis_client.lpush(queue_key, user_id)
30-
print(f"QUEUE: Added {user_id} to queue")
31+
logger.debug(f"Added {user_id} to Queue {(topic, difficulty)}")
3132
queue = await redis_client.lrange(queue_key, 0, -1)
32-
_print_queue_state(topic, difficulty, queue, False)
33+
logger.debug(_get_queue_state_message(topic, difficulty, queue, False))
3334
await release_lock(redis_client, queue_key)
3435
return Response(status_code=202)
3536

3637
# There is a user in the queue
3738
matched_user = await redis_client.rpop(queue_key)
38-
print(f"QUEUE: Match found for {user_id} and {matched_user}")
39+
logger.debug(f"Match found for {user_id} and {matched_user}")
3940
queue = await redis_client.lrange(queue_key, 0, -1)
40-
_print_queue_state(topic, difficulty, queue, False)
41+
logger.debug(_get_queue_state_message(topic, difficulty, queue, False))
4142
await release_lock(redis_client, queue_key)
4243
response = MatchModel(
4344
user1=matched_user,
@@ -61,13 +62,14 @@ async def remove_user_from_queue(
6162
raise Exception("Could not acquire lock")
6263

6364
queue = await redis_client.lrange(queue_key, 0, -1)
64-
_print_queue_state(topic, difficulty, queue, True)
65+
logger.debug(_get_queue_state_message(topic, difficulty, queue, True))
6566

6667
if user_id in queue:
6768
await redis_client.lrem(queue_key, 0, user_id)
68-
print(f"QUEUE: Removed {user_id} from queue")
69-
queue = await redis_client.lrange(queue_key, 0, -1)
70-
_print_queue_state(topic, difficulty, queue, False)
69+
logger.debug(f"Removed {user_id} from queue {(topic, difficulty)}")
70+
71+
queue = await redis_client.lrange(queue_key, 0, -1)
72+
logger.debug(_get_queue_state_message(topic, difficulty, queue, False))
7173

7274
await release_lock(redis_client, queue_key)
7375
await manager.disconnect_all(user_id, topic, difficulty)
@@ -80,8 +82,8 @@ async def remove_user_from_queue(
8082
def _build_queue_key(topic: str, difficulty: str):
8183
return f"{topic}:{difficulty}"
8284

83-
def _print_queue_state(topic, difficulty, queue, before: bool):
85+
def _get_queue_state_message(topic, difficulty, queue, before: bool):
86+
postfix = f"Queue for {(topic, difficulty)}: {queue}"
8487
if before:
85-
print(f"QUEUE: Before Queue for {(topic, difficulty)}: ", queue)
86-
else:
87-
print(f"QUEUE: After Queue for {(topic, difficulty)}: ", queue)
88+
return "Before - " + postfix
89+
return "After - " + postfix

0 commit comments

Comments
 (0)