Skip to content

Commit 80a208c

Browse files
authored
Merge pull request #45 from CS3219-AY2425S1/feat/d4-queue-status-logging
add logging for queue status for D4
2 parents ba41a4a + 0d65622 commit 80a208c

File tree

6 files changed

+36
-3
lines changed

6 files changed

+36
-3
lines changed

backend/matching/src/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ export const DB_URL = `redis://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOSTNAME}:${DB
1212
// export const DB_URL = `redis://${DB_HOSTNAME}:${DB_PORT}`;
1313

1414
export const NODE_ENV = process.env.NODE_ENV;
15+
16+
export const IS_MILESTONE_D4 = true;

backend/matching/src/controllers/match-request.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import type { Request, Response } from 'express';
22
import { StatusCodes } from 'http-status-codes';
33

4-
import { client } from '@/lib/db';
4+
import { client, logQueueStatus } from '@/lib/db';
55
import type { IRedisClient, IRequestMatchPayload } from '@/types';
66
import { createNotifSocket, queueingService } from '@/services';
7+
import { logger } from '@/lib/utils';
78

89
let redisClient: IRedisClient;
910
export const matchRequestController = async (req: Request, res: Response) => {
@@ -39,4 +40,6 @@ export const matchRequestController = async (req: Request, res: Response) => {
3940
socketPort: socketRoom,
4041
timestamp,
4142
});
43+
44+
logQueueStatus(logger, redisClient, `Queue Status Before Matching: <PLACEHOLDER>`);
4245
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './client';
2+
export * from './log-queue-status';
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { IS_MILESTONE_D4 } from '@/config';
2+
3+
import { client } from './client';
4+
import { STREAM_NAME } from './constants';
5+
6+
export const getQueueStatusLog = async (redisClient: typeof client) => {
7+
const queueStatus = await redisClient.xRange(STREAM_NAME, '-', '+');
8+
const messages = queueStatus.map((v) => v.message);
9+
return JSON.stringify(messages);
10+
};
11+
12+
export const logQueueStatus = async (
13+
// eslint-disable-next-line
14+
logger: { info: (...m: any[]) => void },
15+
redisClient: typeof client,
16+
message: string
17+
) => {
18+
if (!IS_MILESTONE_D4) {
19+
return;
20+
}
21+
const queueStatusLog = await getQueueStatusLog(redisClient);
22+
logger.info(message.replace('<PLACEHOLDER>', queueStatusLog));
23+
};

backend/matching/src/workers/cleaner.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { client } from '@/lib/db';
1+
import { client, logQueueStatus } from '@/lib/db';
22
import { STREAM_CLEANER, STREAM_GROUP, STREAM_NAME } from '@/lib/db/constants';
33
import { decodePoolTicket, getPoolKey } from '@/lib/utils';
44
import { MATCH_SVC_EVENT } from '@/ws';
@@ -64,6 +64,8 @@ async function clean() {
6464
sendNotif([socketRoom], MATCH_SVC_EVENT.FAILED);
6565
sendNotif([socketRoom], MATCH_SVC_EVENT.DISCONNECT);
6666
}
67+
68+
await logQueueStatus(logger, redisClient, `Queue Status after Expiring Request: <PLACEHOLDER>`);
6769
}
6870
}
6971

backend/matching/src/workers/matcher.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { client } from '@/lib/db';
1+
import { client, logQueueStatus } from '@/lib/db';
22
import { POOL_INDEX, STREAM_GROUP, STREAM_NAME, STREAM_WORKER } from '@/lib/db/constants';
33
import { decodePoolTicket, getPoolKey, getStreamId } from '@/lib/utils';
44
import { getMatchItems } from '@/services';
@@ -69,6 +69,8 @@ async function processMatch(
6969
const { ...matchItems } = getMatchItems();
7070
sendNotif([requestorSocketPort, matchedSocketPort], MATCH_SVC_EVENT.SUCCESS, matchItems);
7171
sendNotif([requestorSocketPort, matchedSocketPort], MATCH_SVC_EVENT.DISCONNECT);
72+
73+
await logQueueStatus(logger, redisClient, `Queue Status After Matching: <PLACEHOLDER>`);
7274
return true;
7375
}
7476
}

0 commit comments

Comments
 (0)