From a3c7746c96123e366f69499cbf32a9925aecda94 Mon Sep 17 00:00:00 2001 From: You Wei Date: Mon, 21 Oct 2024 01:55:29 +0800 Subject: [PATCH 1/2] Add Match handling --- .../src/services/matchingService.ts | 12 +++---- .../src/services/rabbitMqService.ts | 1 - peerprep-fe/src/app/(main)/match/page.tsx | 33 +++++++++++++++++ peerprep-fe/src/lib/rabbitmq.ts | 36 +++++++++++++++++++ 4 files changed, 73 insertions(+), 9 deletions(-) diff --git a/matching-service/src/services/matchingService.ts b/matching-service/src/services/matchingService.ts index 2a6b3e9bf4..711589a6b8 100644 --- a/matching-service/src/services/matchingService.ts +++ b/matching-service/src/services/matchingService.ts @@ -56,13 +56,11 @@ export const processOldUsers = async (): Promise => { // Send to user subscribed queues if there is a match await sendToQueue(match._id, { status: "matched", - user1: match, - user2: user, + match: user, }); await sendToQueue(user._id, { status: "matched", - user1: match, - user2: user, + match: match, }); continue; } @@ -101,13 +99,11 @@ export const processNewUser = async (user: User): Promise => { if (match) { await sendToQueue(match._id, { status: "matched", - user1: match, - user2: user, + match: user, }); await sendToQueue(user._id, { status: "matched", - user1: match, - user2: user, + match: match, }); } else { // Add to the topic queue if no match diff --git a/matching-service/src/services/rabbitMqService.ts b/matching-service/src/services/rabbitMqService.ts index 3f3e6b2b55..06c74a0bd7 100644 --- a/matching-service/src/services/rabbitMqService.ts +++ b/matching-service/src/services/rabbitMqService.ts @@ -105,7 +105,6 @@ export const sendToQueue = async ( await channel.assertQueue(queue, { durable: true, - expires: 300000, //expire after 5 minutes of idle }); await channel.sendToQueue(queue, Buffer.from(JSON.stringify(payload))); diff --git a/peerprep-fe/src/app/(main)/match/page.tsx b/peerprep-fe/src/app/(main)/match/page.tsx index 480d69c8ce..efe801108e 100644 --- a/peerprep-fe/src/app/(main)/match/page.tsx +++ b/peerprep-fe/src/app/(main)/match/page.tsx @@ -1,11 +1,14 @@ 'use client'; import { useState, useEffect } from 'react'; +import { useRouter } from 'next/router'; import { User, Code } from 'lucide-react'; +import { consumeMessageFromQueue } from '@/lib/rabbitmq'; export default function LoadingPage() { const [elapsedTime, setElapsedTime] = useState(0); const [usersWaiting, setUsersWaiting] = useState(4); + const router = useRouter(); useEffect(() => { const timer = setInterval(() => { @@ -15,6 +18,36 @@ export default function LoadingPage() { return () => clearInterval(timer); }, []); + useEffect(() => { + if (elapsedTime >= 60) { + // Execute your action here + console.log('Elapsed time reached 60 seconds. Going back to main page'); + router.push('/'); + } + }, [elapsedTime]); + + useEffect(() => { + // Start consuming messages from the queue when the component mounts + const startConsumingMessages = async () => { + try { + await consumeMessageFromQueue().then((message) => { + // This function is called when a message is consumed + if (message.status == 'matched') { + console.log('Match found, your partner is'); + router.push('/'); + } else { + console.log('Match failed'); + router.push('/'); + } + }); + } catch (error) { + console.error('Error consuming message:', error); + } + }; + + startConsumingMessages(); + }, []); + return (
diff --git a/peerprep-fe/src/lib/rabbitmq.ts b/peerprep-fe/src/lib/rabbitmq.ts index 44823e2ff3..cebc7b0f59 100644 --- a/peerprep-fe/src/lib/rabbitmq.ts +++ b/peerprep-fe/src/lib/rabbitmq.ts @@ -34,3 +34,39 @@ export const sendMessageToQueue = async (message: Record) => { throw err; } }; + +export const consumeMessageFromQueue = async () => { + return new Promise((resolve, reject) => { + (async () => { + try { + // Connect to RabbitMQ server + const connection = await connect(process.env.RABBITMQ_URL); + const channel = await connection.createChannel(); + const queue = process.env.MATCHING_SERVICE_QUEUE; + + // Ensure the queue exists + await channel.assertQueue(queue, { durable: true }); + + // Consume messages from the queue + console.log(`Waiting for messages in ${queue}...`); + channel.consume( + queue, + (msg: any) => { + if (msg !== null) { + const messageContent = JSON.parse(msg.content.toString()); + console.log(`Received:`, messageContent); + channel.ack(msg); + resolve(messageContent); // Resolve the Promise with the message content + } + }, + { + noAck: false, + }, + ); + } catch (error) { + console.error('Error in consuming messages:', error); + reject(error); // Reject the Promise on error + } + })(); + }); +}; From 6fe25a7b929175b806afdc0821507800b48550f4 Mon Sep 17 00:00:00 2001 From: You Wei Date: Mon, 21 Oct 2024 02:07:30 +0800 Subject: [PATCH 2/2] Fix router bug --- peerprep-fe/src/app/(main)/match/page.tsx | 42 ++++++++++------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/peerprep-fe/src/app/(main)/match/page.tsx b/peerprep-fe/src/app/(main)/match/page.tsx index efe801108e..149aed4a94 100644 --- a/peerprep-fe/src/app/(main)/match/page.tsx +++ b/peerprep-fe/src/app/(main)/match/page.tsx @@ -1,7 +1,7 @@ 'use client'; import { useState, useEffect } from 'react'; -import { useRouter } from 'next/router'; +import { useRouter } from 'next/navigation'; import { User, Code } from 'lucide-react'; import { consumeMessageFromQueue } from '@/lib/rabbitmq'; @@ -10,7 +10,25 @@ export default function LoadingPage() { const [usersWaiting, setUsersWaiting] = useState(4); const router = useRouter(); + const startConsumingMessages = async () => { + try { + await consumeMessageFromQueue().then((message) => { + // This function is called when a message is consumed + if (message.status == 'matched') { + console.log('Match found, your partner is'); + router.push('/'); + } else { + console.log('Match failed'); + router.push('/'); + } + }); + } catch (error) { + console.error('Error consuming message:', error); + } + }; + useEffect(() => { + startConsumingMessages(); const timer = setInterval(() => { setElapsedTime((prevTime) => prevTime + 1); }, 1000); @@ -26,28 +44,6 @@ export default function LoadingPage() { } }, [elapsedTime]); - useEffect(() => { - // Start consuming messages from the queue when the component mounts - const startConsumingMessages = async () => { - try { - await consumeMessageFromQueue().then((message) => { - // This function is called when a message is consumed - if (message.status == 'matched') { - console.log('Match found, your partner is'); - router.push('/'); - } else { - console.log('Match failed'); - router.push('/'); - } - }); - } catch (error) { - console.error('Error consuming message:', error); - } - }; - - startConsumingMessages(); - }, []); - return (