Last active
August 4, 2016 05:08
-
-
Save p-lewis/f9a2eb75996c224f7494 to your computer and use it in GitHub Desktop.
Encode a message in node.js for a Celery worker
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
var uuid = require("node-uuid"); | |
// Create a message to be consumed by the queuedemo.tasks.add Celery task | |
// will incorporate arguments x & y as part of `add(x,y)` message | |
function makeCeleryAddMessage(x, y){ | |
var msgId = uuid.v4(); | |
// This is the outer AMQP envelope | |
var msgEnvelope = { | |
"content-encoding": "utf-8", | |
"content-type": "application/json", | |
headers: {}, | |
properties: { | |
body_encoding: "base64", | |
correlation_id: msgId, | |
delivery_info: {exchange: null, routing_key: null}, | |
delivery_tag: null | |
} | |
} | |
// this is the inner body that Celery needs | |
var body = { | |
task: "queuedemo.tasks.add", // Fully qualified python path to our task | |
args: [x, y], | |
id: msgId, | |
retries: 0 | |
} | |
// encode (JSON and then b64encode) and attache to the envelope as the body | |
msgEnvelope.body = new Buffer(JSON.stringify(body)).toString('base64'); | |
// encode the whole thing again | |
return Buffer(JSON.stringify(msgEnvelope)).toString('base64'); | |
} | |
console.log( | |
makeCeleryAddMessage(7, 3) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment