-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: queue class with rabbitmq intergration (#7)
- Loading branch information
Showing
1 changed file
with
66 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import amqlib from "amqplib/callback_api"; | ||
import { IInvoice } from "interfaces/Invoice.interface"; | ||
import { RabbitMQ_URI } from "../Config"; | ||
|
||
export interface Queues | ||
{ | ||
invoice_paid: IInvoice; | ||
send_email: any; | ||
} | ||
|
||
export default class Queue | ||
{ | ||
private static initialized: boolean = false; | ||
private static connection: amqlib.Connection | undefined; | ||
private static channel: amqlib.Channel | undefined; | ||
|
||
private static init() | ||
{ | ||
if (this.initialized) return; | ||
return new Promise((resolve) => | ||
{ | ||
amqlib.connect(RabbitMQ_URI, async (err, conn) => | ||
{ | ||
if (err) throw err; | ||
this.connection = conn; | ||
// @ts-ignore | ||
this.channel = await conn.createChannel(); | ||
(["invoice_paid"] as const).forEach(async (queue) => | ||
{ | ||
await this.channel?.assertQueue(queue, { durable: true }); | ||
}); | ||
this.initialized = true; | ||
resolve(true); | ||
}); | ||
}); | ||
} | ||
|
||
public static async listen<K extends keyof Queues>(queue: K, callback: (message: Queues[K]) => void) | ||
{ | ||
if (!this.initialized) await this.init(); | ||
this.channel?.consume(queue, async (msg) => | ||
{ | ||
if (msg) | ||
{ | ||
try | ||
{ | ||
const message = JSON.parse(msg.content.toString()); | ||
callback(message); | ||
this.channel?.ack(msg); | ||
} | ||
catch (err) | ||
{ | ||
console.error(err); | ||
} | ||
} | ||
}, { noAck: false }); | ||
} | ||
|
||
public static async send<K extends keyof Queues>(queue: K, json: Queues[K] extends Array<infer U> ? U : never) | ||
{ | ||
if (!this.initialized) await this.init(); | ||
const data = JSON.stringify(json); | ||
this.channel?.sendToQueue(queue, Buffer.from(data)); | ||
} | ||
|
||
} |