Created
June 6, 2016 15:07
-
-
Save yunghoy/79adce6b58241e475551bd48ec84f85f to your computer and use it in GitHub Desktop.
simple work queue
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
// new_task.js | |
#!/usr/bin/env node | |
var amqp = require('amqplib/callback_api'); | |
amqp.connect('amqp://localhost', function(err, conn) { | |
conn.createChannel(function(err, ch) { | |
var q = 'task_queue'; | |
var msg = process.argv.slice(2).join(' ') || "Hello World!"; | |
ch.assertQueue(q, {durable: true}); | |
ch.sendToQueue(q, new Buffer(msg), {persistent: true}); | |
console.log(" [x] Sent '%s'", msg); | |
}); | |
setTimeout(function() { conn.close(); process.exit(0) }, 500); | |
}); | |
// worker.js | |
#!/usr/bin/env node | |
var amqp = require('amqplib/callback_api'); | |
amqp.connect('amqp://localhost', function(err, conn) { | |
conn.createChannel(function(err, ch) { | |
var q = 'task_queue'; | |
ch.assertQueue(q, {durable: true}); | |
ch.prefetch(1); | |
console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", q); | |
ch.consume(q, function(msg) { | |
var secs = msg.content.toString().split('.').length - 1; | |
console.log(" [x] Received %s", msg.content.toString()); | |
setTimeout(function() { | |
console.log(" [x] Done"); | |
ch.ack(msg); | |
}, secs * 1000); | |
}, {noAck: false}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment