Skip to content

Commit 49f57f6

Browse files
committed
Add queue debug
1 parent 2c81e6f commit 49f57f6

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

matching-service/app/logic/matching.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,28 @@ async def find_match_else_enqueue(
1616
if not islocked:
1717
raise Exception("Could not acquire lock")
1818

19+
queue = await redis_client.lrange(queue_key, 0, -1)
20+
_print_queue_state(topic, difficulty, queue, True)
21+
1922
# Check if the user is already in the queue
20-
if user_id in await redis_client.lrange(queue_key, 0, -1):
23+
if user_id in queue:
2124
await release_lock(redis_client, queue_key)
2225
return Response(status_code=304)
2326

2427
# Check if there are no other users in the queue
2528
if await redis_client.llen(queue_key) == 0:
2629
await redis_client.lpush(queue_key, user_id)
30+
print(f"QUEUE: Added {user_id} to queue")
31+
queue = await redis_client.lrange(queue_key, 0, -1)
32+
_print_queue_state(topic, difficulty, queue, False)
2733
await release_lock(redis_client, queue_key)
2834
return Response(status_code=202)
2935

3036
# There is a user in the queue
3137
matched_user = await redis_client.rpop(queue_key)
38+
print(f"QUEUE: Match found for {user_id} and {matched_user}")
39+
queue = await redis_client.lrange(queue_key, 0, -1)
40+
_print_queue_state(topic, difficulty, queue, False)
3241
await release_lock(redis_client, queue_key)
3342
response = MatchModel(
3443
user1=matched_user,
@@ -51,8 +60,14 @@ async def remove_user_from_queue(
5160
if not islocked:
5261
raise Exception("Could not acquire lock")
5362

54-
if user_id in await redis_client.lrange(queue_key, 0, -1):
63+
queue = await redis_client.lrange(queue_key, 0, -1)
64+
_print_queue_state(topic, difficulty, queue, True)
65+
66+
if user_id in queue:
5567
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)
5671

5772
await release_lock(redis_client, queue_key)
5873
await manager.disconnect_all(user_id, topic, difficulty)
@@ -64,3 +79,9 @@ async def remove_user_from_queue(
6479
# Builds a queue key based on topic and difficulty
6580
def _build_queue_key(topic: str, difficulty: str):
6681
return f"{topic}:{difficulty}"
82+
83+
def _print_queue_state(topic, difficulty, queue, before: bool):
84+
if before:
85+
print(f"QUEUE: Before Queue for {(topic, difficulty)}: ", queue)
86+
else:
87+
print(f"QUEUE: After Queue for {(topic, difficulty)}: ", queue)
File renamed without changes.

0 commit comments

Comments
 (0)