51 lines
1.7 KiB
JavaScript
51 lines
1.7 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
const amqp = require('amqplib');
|
||
|
const {Service} = require("@hapipal/schmervice");
|
||
|
|
||
|
module.exports = class RabbitmqService extends Service {
|
||
|
async sendCsvToQueue() {
|
||
|
try {
|
||
|
const connection = await amqp.connect('amqp://localhost'); // Assurez-vous d'ajuster l'URL de connexion si nécessaire
|
||
|
const channel = await connection.createChannel();
|
||
|
const queue = 'csv_queue';
|
||
|
|
||
|
await channel.assertQueue(queue, { durable: false });
|
||
|
await channel.sendToQueue(queue, Buffer.from('Send the CSV PLZ!'));
|
||
|
|
||
|
await channel.close();
|
||
|
await connection.close();
|
||
|
} catch (error) {
|
||
|
console.error('Erreur lors de l\'envoi du CSV à la file d\'attente RabbitMQ :', error);
|
||
|
throw error;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async receiveAndReturnCSV() {
|
||
|
try {
|
||
|
|
||
|
const connection = await amqp.connect('amqp://localhost'); // Assurez-vous d'ajuster l'URL de connexion si nécessaire
|
||
|
const channel = await connection.createChannel();
|
||
|
const queue = 'csv_queue';
|
||
|
|
||
|
await channel.assertQueue(queue, { durable: false });
|
||
|
console.log('En attente de messages dans la file d\'attente RabbitMQ...');
|
||
|
|
||
|
//when message is received, send the csv to the admin email
|
||
|
await channel.consume(queue, async (message) => {
|
||
|
return message.content.toString();
|
||
|
|
||
|
});
|
||
|
|
||
|
await channel.close();
|
||
|
await connection.close();
|
||
|
|
||
|
} catch (error) {
|
||
|
console.error('Erreur lors de la réception du CSV de la file d\'attente RabbitMQ :', error);
|
||
|
throw error;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
};
|