UNPKG

hrms-shared

Version:

HRMS shared code: models, middleware, utils

30 lines (23 loc) 634 B
const amqp = require("amqplib"); let conn = null; let channel = null; let connecting = null; async function getRabbitChannel() { if (channel) return channel; if (!connecting) { connecting = (async () => { conn = await amqp.connect(process.env.AMQP_URL || "amqp://localhost"); channel = await conn.createChannel(); return channel; })(); } return connecting; }; process.on("SIGINT", async () => { if (conn) { await conn.close(); console.log("RabbitMQ connection closed"); } process.exit(0); }); module.exports = { getRabbitChannel };