implement rabbitmq to todo service

This commit is contained in:
Kfir Dayan 2023-07-08 10:56:00 +03:00
parent 6253d0360c
commit 44feb4d01c
6 changed files with 55 additions and 6 deletions

View file

@ -7,7 +7,7 @@
"moduleResolution": "node", "moduleResolution": "node",
"sourceMap": true, "sourceMap": true,
"outDir": "./dist", "outDir": "./dist",
"rootDir": "./src", "rootDir": "./src"
"noImplicitAny": true, // "noImplicitAny": true,
} }
} }

View file

@ -9,7 +9,7 @@ Content-Type: application/json
{ {
"title": "Go to the gym", "title": "Go to the gym",
"description": "Go to the gym at 8pm", "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 ### update request

View file

@ -10,12 +10,14 @@
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@aws-sdk/types": "^3.357.0", "@aws-sdk/types": "^3.357.0",
"amqplib": "^0.10.3",
"aws-sdk": "^2.1413.0", "aws-sdk": "^2.1413.0",
"dotenv": "^16.3.1", "dotenv": "^16.3.1",
"express": "^4.18.2", "express": "^4.18.2",
"mongoose": "^7.3.1" "mongoose": "^7.3.1"
}, },
"devDependencies": { "devDependencies": {
"@types/amqplib": "^0.10.1",
"@types/express": "^4.17.17" "@types/express": "^4.17.17"
} }
}, },

View file

@ -11,12 +11,14 @@
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@aws-sdk/types": "^3.357.0", "@aws-sdk/types": "^3.357.0",
"amqplib": "^0.10.3",
"aws-sdk": "^2.1413.0", "aws-sdk": "^2.1413.0",
"dotenv": "^16.3.1", "dotenv": "^16.3.1",
"express": "^4.18.2", "express": "^4.18.2",
"mongoose": "^7.3.1" "mongoose": "^7.3.1"
}, },
"devDependencies": { "devDependencies": {
"@types/amqplib": "^0.10.1",
"@types/express": "^4.17.17" "@types/express": "^4.17.17"
} }
} }

View file

@ -2,17 +2,18 @@ import { NextFunction, Request, Response } from 'express';
import { ApiError } from '../utils/ApiError'; import { ApiError } from '../utils/ApiError';
import { ITodo } from '../schemas/todoSchema'; import { ITodo } from '../schemas/todoSchema';
import { TodoModel } from '../models/todoModel'; 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; const env = require('dotenv').config().parsed;
export class TodoController { export class TodoController {
private todoModel: TodoModel; private todoModel: TodoModel;
queue: Sqs; queue: RabbitMQ;
constructor() { constructor() {
this.todoModel = new TodoModel(); this.todoModel = new TodoModel();
this.queue = new Sqs(); this.queue = new RabbitMQ();
} }
public getAll = async (req: Request, res: Response, next: NextFunction) => { public getAll = async (req: Request, res: Response, next: NextFunction) => {

View file

@ -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;
}
}
}