@@ -5,15 +5,24 @@ const matching_exchange_name = "matching_exchange";
5
5
const dead_letter_exchange_name = "dead_letter_exchange" ;
6
6
const dead_letter_queue_name = "dead_letter_queue" ;
7
7
const cancel_queue_name = "cancel_queue" ;
8
- const difficulties = [ "easy" , "medium" , "hard" , "any" ] ; // Add "any" for fallback
9
- const categories = [ "data-structures" , "strings" , "arrays" ] ; // Adjust to include categories
10
-
11
- const queueNames = generator . generateQueueNames (
12
- generator . generateCombinations ( difficulties , categories )
13
- ) ;
8
+ const difficulties = [ "easy" , "medium" , "hard" , "any" ] ;
9
+ const axios = require ( 'axios' ) ;
10
+ const categoryAPIUrl = 'http://localhost:3001/api/categories' ;
11
+
12
+ let queueNamesPromise = axios . get ( categoryAPIUrl )
13
+ . then ( response => {
14
+ const categories = response . data . map ( category => category . name . toLowerCase ( ) . replace ( / \s + / g, '-' ) ) ;
15
+ console . log ( "categories from api: " , categories )
16
+ return generator . generateQueueNames ( generator . generateCombinations ( difficulties , categories ) ) ;
17
+ } )
18
+ . catch ( error => {
19
+ console . error ( "Error fetching categories:" , error ) ;
20
+ return [ ] ; // Return an empty array if categories couldn't be fetched
21
+ } ) ;
14
22
15
23
async function setupRabbitMQ ( ) {
16
24
try {
25
+ const queueNames = await queueNamesPromise ;
17
26
const connection = await amqp . connect ( process . env . RABBITMQ_URL ) ;
18
27
19
28
if ( ! connection ) {
@@ -30,29 +39,27 @@ async function setupRabbitMQ() {
30
39
31
40
// Declare and bind all main queues with TTL and DLQ bindings
32
41
for ( let queueName of queueNames ) {
33
- await channel . deleteQueue ( queueName ) ; // Ensure we start fresh for each setup
42
+ await channel . deleteQueue ( queueName ) ; // Ensure we start fresh for each setup
34
43
35
44
await channel . assertQueue ( queueName , {
36
45
durable : true ,
37
46
arguments : {
38
- 'x-message-ttl' : 10000 , // 60 seconds TTL
39
- 'x-dead-letter-exchange' : dead_letter_exchange_name // Bind to dead-letter exchange
47
+ 'x-message-ttl' : 10000 , // TTL for messages in the queue
48
+ 'x-dead-letter-exchange' : dead_letter_exchange_name // Bind to dead-letter exchange
40
49
}
41
50
} ) ;
42
51
43
52
await channel . bindQueue ( queueName , matching_exchange_name , queueName ) ; // Bind to exchange
44
53
}
45
54
46
- // Delete DLQ before asserting it
55
+ // Delete and recreate the DLQ
47
56
await channel . deleteQueue ( dead_letter_queue_name ) ;
48
-
49
- // Declare the dead-letter queue and bind it to the dead-letter exchange
50
57
await channel . assertQueue ( dead_letter_queue_name , { durable : true } ) ;
51
58
await channel . bindQueue ( dead_letter_queue_name , dead_letter_exchange_name , '' ) ; // Bind with no routing key
52
59
53
- // Declare and bind the cancel queue
54
- await channel . deleteQueue ( cancel_queue_name ) ; // Delete any existing cancel queue
55
- await channel . assertQueue ( cancel_queue_name , { durable : true } ) ; // Declare the cancel queue
60
+ // Delete and recreate the cancel queue
61
+ await channel . deleteQueue ( cancel_queue_name ) ;
62
+ await channel . assertQueue ( cancel_queue_name , { durable : true } ) ;
56
63
await channel . bindQueue ( cancel_queue_name , matching_exchange_name , 'cancel' ) ; // Bind with the "cancel" routing key
57
64
58
65
console . log ( "RabbitMQ setup complete with queues, DLQ, and bindings." ) ;
@@ -64,4 +71,4 @@ async function setupRabbitMQ() {
64
71
}
65
72
}
66
73
67
- module . exports = { setupRabbitMQ, matching_exchange_name, queueNames , dead_letter_queue_name , cancel_queue_name} ;
74
+ module . exports = { setupRabbitMQ, matching_exchange_name, queueNamesPromise , dead_letter_queue_name, cancel_queue_name } ;
0 commit comments