From 8bc7e1d8a0074484966f2578342bb1dc715cca75 Mon Sep 17 00:00:00 2001 From: Kfir Dayan Date: Sat, 8 Jul 2023 11:39:54 +0300 Subject: [PATCH] done implementing rabbit + need to fix delay bug --- notification-service/src/main.ts | 13 ++++--------- notification-service/src/rabbitmq/RabbitMQ.ts | 4 ++++ request.http | 2 +- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/notification-service/src/main.ts b/notification-service/src/main.ts index 09ffde4..a8463a6 100644 --- a/notification-service/src/main.ts +++ b/notification-service/src/main.ts @@ -6,18 +6,13 @@ export class NotificationService { constructor() { this.rabbitmq = new RabbitMQ(); - this.rabbitmq.connect().then(() => { - console.log('RabbitMQ connected'); - }).catch((error) => { - console.error('Error connecting to RabbitMQ:', error); - }); - + this.startListener(); } - startListener() { + async startListener() { + await this.rabbitmq.connect(); this.rabbitmq.startConsumer(); } } -const notificationService = new NotificationService(); -notificationService.startListener(); +const notificationService = new NotificationService(); \ No newline at end of file diff --git a/notification-service/src/rabbitmq/RabbitMQ.ts b/notification-service/src/rabbitmq/RabbitMQ.ts index 4402b63..8dfecbc 100644 --- a/notification-service/src/rabbitmq/RabbitMQ.ts +++ b/notification-service/src/rabbitmq/RabbitMQ.ts @@ -44,6 +44,10 @@ export class RabbitMQ { async startConsumer() { try { + if (!this.channel) { + throw new Error('Channel is not initialized. Make sure to call connect before starting the consumer.'); + } + await this.channel.prefetch(1); console.log('Consumer started, waiting for messages...'); this.channel.consume(this.queueName, async (message) => { diff --git a/request.http b/request.http index 5ce3897..a2a08ef 100644 --- a/request.http +++ b/request.http @@ -7,7 +7,7 @@ POST http://localhost:3000/todo Content-Type: application/json { - "title": "Go to the gym", + "title": "Go to the gymNEW!!!", "description": "Go to the gym at 8pm", "due_date": "2023-07-08 12:00:00" }