Last active
August 16, 2021 12:35
-
-
Save homanp/88b11d2e040eafe2f093f6b090a5afb5 to your computer and use it in GitHub Desktop.
CloudAMQP Sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| '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