mirror of
https://github.com/serverless/serverless.git
synced 2026-01-18 14:58:43 +00:00
Co-authored-by: Michael <michael@Michaels-MacBook-Pro.local> Co-authored-by: Michael <michael@ip-192-168-158-61.eu-west-1.compute.internal>
44 lines
1.2 KiB
JavaScript
44 lines
1.2 KiB
JavaScript
'use strict';
|
|
|
|
// NOTE: `amqplib` is bundled into the deployment package
|
|
// eslint-disable-next-line import/no-unresolved
|
|
const amqp = require('amqplib');
|
|
|
|
function consumer(event, context, callback) {
|
|
const functionName = 'consumer';
|
|
const messages = [];
|
|
|
|
Object.keys(event.rmqMessagesByQueue).forEach((queueKey) => {
|
|
const queue = event.rmqMessagesByQueue[queueKey];
|
|
queue.forEach((message) => {
|
|
messages.push(Buffer.from(message.data, 'base64').toString());
|
|
});
|
|
});
|
|
// eslint-disable-next-line no-console
|
|
console.log(functionName, JSON.stringify(messages));
|
|
|
|
return callback(null, event);
|
|
}
|
|
|
|
async function producer() {
|
|
const connectOptions = {
|
|
protocol: 'amqps',
|
|
hostname: process.env.RABBITMQ_HOST,
|
|
port: 5671,
|
|
username: process.env.RABBITMQ_USERNAME,
|
|
password: process.env.RABBITMQ_PASSWORD,
|
|
};
|
|
|
|
const connection = await amqp.connect(connectOptions);
|
|
const channel = await connection.createChannel();
|
|
const queueName = process.env.QUEUE_NAME;
|
|
await channel.assertQueue(queueName);
|
|
await channel.sendToQueue(queueName, Buffer.from('Hello from RabbitMQ Integration test!'));
|
|
|
|
return {
|
|
statusCode: 200,
|
|
};
|
|
}
|
|
|
|
module.exports = { producer, consumer };
|