|
1 | 1 | import amqplib, { Connection } from "amqplib";
|
2 | 2 | import dotenv from "dotenv";
|
3 |
| -import { matchUsers } from "../src/utils/mq_utils"; |
| 3 | +import { matchUsers, matchUsersInQueue } from "../src/utils/mq_utils"; |
4 | 4 | import { MatchRequestItem } from "../src/handlers/matchHandler";
|
5 | 5 |
|
6 | 6 | dotenv.config();
|
7 | 7 |
|
8 | 8 | let mrConnection: Connection;
|
9 | 9 | const queue = "match_requests";
|
10 | 10 |
|
| 11 | +let mrConnectionNew: Connection; |
| 12 | +const queues: string[] = []; |
| 13 | + |
| 14 | +enum Complexities { |
| 15 | + EASY = "Easy", |
| 16 | + MEDIUM = "Medium", |
| 17 | + HARD = "Hard", |
| 18 | +} |
| 19 | + |
| 20 | +enum Categories { |
| 21 | + STRINGS = "Strings", |
| 22 | + ALGORITHMS = "Algorithms", |
| 23 | + DATA_STRUCTURES = "Data Structures", |
| 24 | + BIT_MANIPULATION = "Bit Manipulation", |
| 25 | + RECURSION = "Recursion", |
| 26 | + DYNAMIC_PROGRAMMING = "Dynamic Programming", |
| 27 | + ARRAYS = "Arrays", |
| 28 | + TREE = "Tree", |
| 29 | +} |
| 30 | + |
| 31 | +enum LANGUAGES { |
| 32 | + PYTHON = "Python", |
| 33 | + JAVA = "Java", |
| 34 | + C = "C", |
| 35 | +} |
| 36 | + |
| 37 | +export const pendingRequestsPerQueue = new Map< |
| 38 | + string, |
| 39 | + Map<string, MatchRequestItem> |
| 40 | +>(); |
| 41 | + |
| 42 | +const initQueueNames = () => { |
| 43 | + for (const complexity of Object.values(Complexities)) { |
| 44 | + for (const category of Object.values(Categories)) { |
| 45 | + for (const language of Object.values(LANGUAGES)) { |
| 46 | + queues.push(`${complexity}_${category}_${language}`); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | +}; |
| 51 | + |
| 52 | +export const connectToRabbitMq = async () => { |
| 53 | + try { |
| 54 | + initQueueNames(); |
| 55 | + mrConnectionNew = await amqplib.connect(`${process.env.RABBITMQ_ADDR}`); |
| 56 | + for (const queue of queues) { |
| 57 | + await setUpQueue(queue); |
| 58 | + pendingRequestsPerQueue.set(queue, new Map<string, MatchRequestItem>()); |
| 59 | + } |
| 60 | + } catch (error) { |
| 61 | + console.error(error); |
| 62 | + process.exit(1); |
| 63 | + } |
| 64 | +}; |
| 65 | + |
| 66 | +const setUpQueue = async (queueName: string) => { |
| 67 | + const consumerChannel = await mrConnectionNew.createChannel(); |
| 68 | + await consumerChannel.assertQueue(queueName); |
| 69 | + |
| 70 | + consumerChannel.consume(queueName, (msg) => { |
| 71 | + console.log(`consume from queue: ${queueName}`); |
| 72 | + if (msg !== null) { |
| 73 | + matchUsersInQueue(queueName, msg.content.toString()); |
| 74 | + consumerChannel.ack(msg); |
| 75 | + } |
| 76 | + }); |
| 77 | +}; |
| 78 | + |
| 79 | +export const sendToQueue = async (data: MatchRequestItem) => { |
| 80 | + try { |
| 81 | + const queueName = `${data.complexities[0]}_${data.categories[0]}_${data.languages[0]}`; |
| 82 | + const senderChannel = await mrConnectionNew.createChannel(); |
| 83 | + senderChannel.sendToQueue(queueName, Buffer.from(JSON.stringify(data))); |
| 84 | + return true; |
| 85 | + } catch (error) { |
| 86 | + console.log(error); |
| 87 | + return false; |
| 88 | + } |
| 89 | +}; |
| 90 | + |
| 91 | +// ---------------- |
| 92 | + |
11 | 93 | export const connectRabbitMq = async () => {
|
12 | 94 | try {
|
13 | 95 | mrConnection = await amqplib.connect(`${process.env.RABBITMQ_ADDR}`);
|
|
0 commit comments