beautifying

This commit is contained in:
Kfir Dayan 2023-07-13 11:01:02 +03:00
parent 6bddf68d04
commit 6e34384129
2 changed files with 6 additions and 26 deletions

View file

@ -12,6 +12,7 @@ export class NotificationService {
this.currentDate = DateService.getInstance(); this.currentDate = DateService.getInstance();
this.rabbitmq = new RabbitMQ(); this.rabbitmq = new RabbitMQ();
this.mongoModel = new MongoDbModel(); this.mongoModel = new MongoDbModel();
this.startListener(); this.startListener();
} }
@ -45,5 +46,3 @@ export class NotificationService {
console.log("Sending notification for Todo:", todo._id); console.log("Sending notification for Todo:", todo._id);
} }
} }
const notificationService = new NotificationService();

View file

@ -13,7 +13,7 @@ export class RabbitMQ {
constructor() { constructor() {
this.envService = EnvService.getInstance(); this.envService = EnvService.getInstance();
this.queueName = this.envService.getEnvVariable('RABBITMQ_QUEUE_NAME'); this.queueName = this.envService.getEnvVariable("RABBITMQ_QUEUE_NAME");
this.mongoClient = new MongoDbModel(); this.mongoClient = new MongoDbModel();
@ -30,10 +30,10 @@ export class RabbitMQ {
try { try {
const connection = await amqp.connect({ const connection = await amqp.connect({
protocol: "amqp", protocol: "amqp",
hostname: this.envService.getEnvVariable('RABBITMQ_HOST'), hostname: this.envService.getEnvVariable("RABBITMQ_HOST"),
port: parseInt(this.envService.getEnvVariable('RABBITMQ_PORT')), port: parseInt(this.envService.getEnvVariable("RABBITMQ_PORT")),
username: this.envService.getEnvVariable('RABBITMQ_USERNAME'), username: this.envService.getEnvVariable("RABBITMQ_USERNAME"),
password: this.envService.getEnvVariable('RABBITMQ_PASSWORD'), password: this.envService.getEnvVariable("RABBITMQ_PASSWORD"),
}); });
this.channel = await connection.createChannel(); this.channel = await connection.createChannel();
@ -44,25 +44,6 @@ export class RabbitMQ {
} }
} }
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
);
} catch (error) {
console.error("Error sending message to RabbitMQ:", error);
throw error;
}
}
async startConsumer(callback: Function): Promise<ITodo | void> { async startConsumer(callback: Function): Promise<ITodo | void> {
this.channel.assertQueue(this.queueName); this.channel.assertQueue(this.queueName);
this.channel.consume(this.queueName, (message: ConsumeMessage | null) => { this.channel.consume(this.queueName, (message: ConsumeMessage | null) => {