Skip to content

Instantly share code, notes, and snippets.

@homanp
Last active August 16, 2021 12:35
Show Gist options
  • Select an option

  • Save homanp/88b11d2e040eafe2f093f6b090a5afb5 to your computer and use it in GitHub Desktop.

Select an option

Save homanp/88b11d2e040eafe2f093f6b090a5afb5 to your computer and use it in GitHub Desktop.
CloudAMQP Sample
'use strict';
const amqp = require('amqplib');
const QUEUE = 'pdf-generator';
module.exports = async (request, response) => {
const {projectId} = request.body;
if (!url) {
return response.status(500).json('Please provide a project ID');
}
try {
// The URL can be found in the CLOUDAMQP account
const connection = await amqp.connect(process.env.CLOUDAMQP_URL);
const channel = await connection.createChannel();
const ok = await channel.assertQueue(QUEUE);
if (!ok) {
throw new Error('Failed asserting queue');
}
await channel.sendToQueue(QUEUE, Buffer.from(projectId));
return response.status(200).json({success: true});
} catch (error) {
return response.status(500).json(`Error queueing task: ${error.message}`);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment