Skip to content

Instantly share code, notes, and snippets.

@p-lewis
Last active August 4, 2016 05:08
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save p-lewis/f9a2eb75996c224f7494 to your computer and use it in GitHub Desktop.
Save p-lewis/f9a2eb75996c224f7494 to your computer and use it in GitHub Desktop.
Encode a message in node.js for a Celery worker
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