From 44feb4d01cbd5df1d118f8d6573cf583b33ba04e Mon Sep 17 00:00:00 2001 From: Kfir Dayan Date: Sat, 8 Jul 2023 10:56:00 +0300 Subject: [PATCH] implement rabbitmq to todo service --- notification-service/tsconfig.json | 4 +- request.http | 2 +- todo-service/package-lock.json | 2 + todo-service/package.json | 2 + .../src/controllers/todoController.ts | 7 +-- todo-service/src/rabbitmq/RabbitMQ.ts | 44 +++++++++++++++++++ 6 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 todo-service/src/rabbitmq/RabbitMQ.ts diff --git a/notification-service/tsconfig.json b/notification-service/tsconfig.json index d04ff3c..959ecfc 100644 --- a/notification-service/tsconfig.json +++ b/notification-service/tsconfig.json @@ -7,7 +7,7 @@ "moduleResolution": "node", "sourceMap": true, "outDir": "./dist", - "rootDir": "./src", - "noImplicitAny": true, + "rootDir": "./src" + // "noImplicitAny": true, } } \ No newline at end of file diff --git a/request.http b/request.http index c7d2397..77b48ef 100644 --- a/request.http +++ b/request.http @@ -9,7 +9,7 @@ Content-Type: application/json { "title": "Go to the gym", "description": "Go to the gym at 8pm", - "due_date": "2023-07-08 01:21:00" + "due_date": "2023-07-08 10:55:00" } ### update request diff --git a/todo-service/package-lock.json b/todo-service/package-lock.json index a6650fb..85ced60 100644 --- a/todo-service/package-lock.json +++ b/todo-service/package-lock.json @@ -10,12 +10,14 @@ "license": "ISC", "dependencies": { "@aws-sdk/types": "^3.357.0", + "amqplib": "^0.10.3", "aws-sdk": "^2.1413.0", "dotenv": "^16.3.1", "express": "^4.18.2", "mongoose": "^7.3.1" }, "devDependencies": { + "@types/amqplib": "^0.10.1", "@types/express": "^4.17.17" } }, diff --git a/todo-service/package.json b/todo-service/package.json index 4567695..d7e954d 100644 --- a/todo-service/package.json +++ b/todo-service/package.json @@ -11,12 +11,14 @@ "license": "ISC", "dependencies": { "@aws-sdk/types": "^3.357.0", + "amqplib": "^0.10.3", "aws-sdk": "^2.1413.0", "dotenv": "^16.3.1", "express": "^4.18.2", "mongoose": "^7.3.1" }, "devDependencies": { + "@types/amqplib": "^0.10.1", "@types/express": "^4.17.17" } } diff --git a/todo-service/src/controllers/todoController.ts b/todo-service/src/controllers/todoController.ts index 4280999..667934d 100644 --- a/todo-service/src/controllers/todoController.ts +++ b/todo-service/src/controllers/todoController.ts @@ -2,17 +2,18 @@ import { NextFunction, Request, Response } from 'express'; import { ApiError } from '../utils/ApiError'; import { ITodo } from '../schemas/todoSchema'; import { TodoModel } from '../models/todoModel'; -import { Sqs } from '../aws/Sqs'; +// import { Sqs } from '../aws/Sqs'; +import { RabbitMQ } from '../rabbitmq/RabbitMQ'; const env = require('dotenv').config().parsed; export class TodoController { private todoModel: TodoModel; - queue: Sqs; + queue: RabbitMQ; constructor() { this.todoModel = new TodoModel(); - this.queue = new Sqs(); + this.queue = new RabbitMQ(); } public getAll = async (req: Request, res: Response, next: NextFunction) => { diff --git a/todo-service/src/rabbitmq/RabbitMQ.ts b/todo-service/src/rabbitmq/RabbitMQ.ts new file mode 100644 index 0000000..da6aeb3 --- /dev/null +++ b/todo-service/src/rabbitmq/RabbitMQ.ts @@ -0,0 +1,44 @@ +import amqp from 'amqplib'; +import { ITodo } from '../schemas/todoSchema'; + +const env = require('dotenv').config().parsed; + +export class RabbitMQ { + channel: amqp.Channel; + queueName: string; + + constructor() { + this.queueName = env.RABBITMQ_QUEUE_NAME; + + this.connect().then(() => { + console.log('RabbitMQ connected'); + }).catch((error) => { + console.error('Error connecting to RabbitMQ:', error); + }); + } + + async connect() { + const connection = await amqp.connect({ + protocol: 'amqp', + hostname: env.RABBITMQ_HOST, + port: parseInt(env.RABBITMQ_PORT), + username: env.RABBITMQ_USERNAME, + password: env.RABBITMQ_PASSWORD, + }); + this.channel = await connection.createChannel(); + await this.channel.assertQueue(this.queueName, { durable: true }); + } + + async create(payload: ITodo, delayTimeForQueue: number) { + const message = JSON.stringify({ payload, delayTimeForQueue }); + const options = { persistent: true, expiration: delayTimeForQueue.toString() }; + + try { + await this.channel.sendToQueue(this.queueName, Buffer.from(message), options); + console.log('Message sent to the queue'); + } catch (error) { + console.error('Error sending message to RabbitMQ:', error); + throw error; + } + } +}