-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement queue configs for email sending
- Loading branch information
Showing
9 changed files
with
141 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import amqp, { Channel } from "amqplib"; | ||
import { configs } from "../config"; | ||
import EmailService from "./email.handler"; | ||
|
||
// Create a channel | ||
const createChannel = async () => { | ||
try { | ||
const connection = await amqp.connect(configs.queue.messageBrokerURL); | ||
const channel = await connection.createChannel(); | ||
await channel.assertExchange(configs.queue.exchangeName, "direct", { durable: false }); | ||
return channel; | ||
} catch (error) { | ||
throw error; | ||
} | ||
}; | ||
|
||
// Publish the messages | ||
const publishMessageToQueue = async (channel: Channel, bindingKey: string, message: any) => { | ||
try { | ||
await channel.publish(configs.queue.exchangeName, bindingKey, Buffer.from(JSON.stringify(message))); | ||
} catch (error) { | ||
throw error; | ||
} | ||
}; | ||
|
||
// Subscribe to messages | ||
const subscribeMessages = async (channel: Channel, service: EmailService) => { | ||
const serviceQueue = await channel.assertQueue(configs.queue.emailQueue); | ||
channel.bindQueue(serviceQueue.queue, configs.queue.exchangeName, configs.queue.emailService); | ||
channel.consume(serviceQueue.queue, (data) => { | ||
if (data) { | ||
const queueItem = JSON.parse(JSON.parse(data.content.toString())); | ||
service.sendEmailWithTemplate(queueItem); | ||
channel.ack(data); // Send acknowledgement to queue after consume the message | ||
} | ||
}); | ||
}; | ||
|
||
export { createChannel, publishMessageToQueue, subscribeMessages }; |