1
1
from fastapi .responses import JSONResponse , Response
2
2
from typing import Union
3
3
4
+ from logger import logger
4
5
from models .match import MatchModel , MessageModel
5
6
from utils .redis import acquire_lock , redis_client , release_lock
6
7
from utils .socketmanager import manager
@@ -17,7 +18,7 @@ async def find_match_else_enqueue(
17
18
raise Exception ("Could not acquire lock" )
18
19
19
20
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 ) )
21
22
22
23
# Check if the user is already in the queue
23
24
if user_id in queue :
@@ -27,17 +28,17 @@ async def find_match_else_enqueue(
27
28
# Check if there are no other users in the queue
28
29
if await redis_client .llen (queue_key ) == 0 :
29
30
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 ) } " )
31
32
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 ) )
33
34
await release_lock (redis_client , queue_key )
34
35
return Response (status_code = 202 )
35
36
36
37
# There is a user in the queue
37
38
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 } " )
39
40
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 ) )
41
42
await release_lock (redis_client , queue_key )
42
43
response = MatchModel (
43
44
user1 = matched_user ,
@@ -61,13 +62,14 @@ async def remove_user_from_queue(
61
62
raise Exception ("Could not acquire lock" )
62
63
63
64
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 ) )
65
66
66
67
if user_id in queue :
67
68
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 ))
71
73
72
74
await release_lock (redis_client , queue_key )
73
75
await manager .disconnect_all (user_id , topic , difficulty )
@@ -80,8 +82,8 @@ async def remove_user_from_queue(
80
82
def _build_queue_key (topic : str , difficulty : str ):
81
83
return f"{ topic } :{ difficulty } "
82
84
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 } "
84
87
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