🐰 RabbitMQ for import job first draft

This commit is contained in:
2021-09-13 22:06:19 -07:00
parent 4a39912b2f
commit e9fb359ca3
8 changed files with 353 additions and 81 deletions

34
queue/importQueue.ts Normal file
View File

@@ -0,0 +1,34 @@
import { logger } from "../utils/logger.utils";
//RabbitMQ
const amqp = require("amqplib/callback_api");
const rabbitUrl = "amqp://localhost";
export const sendRabbitMQ = (queueName, data) => {
// connect to local rabbitmq instance
amqp.connect(rabbitUrl, (error0, connection) => {
if (error0) {
throw error0;
}
// create channel
connection.createChannel((error1, channel) => {
if (error1) {
throw error1;
}
channel.prefetch(1);
const queue = queueName;
// Checks for “queueName (updateStock)” queue. If it doesnt exist, then it creates one.
channel.assertQueue(queue, {
durable: false,
});
channel.sendToQueue(queue, Buffer.from(data));
logger.info(`${data} sent`);
});
setTimeout(function () {
connection.close();
//process.exit(0);
}, 500);
});
};
module.exports = sendRabbitMQ;