I am trying to set up a Node.js job queue on Heroku. I am using RabbitMQ with the jackrabbit npm module for this task.
After following the example in the repository, I have the following files:
server.js. Node simple web server.
worker.js. Worker.
As I understand it, on Heroku I start a web process and a worker process in the Procfile:
// Procfile
web: node server.js
worker: node worker.js
In the worker.js file, I do the following:
// worker.js
var http = require('http');
var throng = require('throng');
var jackrabbit = require('jackrabbit')
// want to use "worker: node worker.js" in heroku Procfile
var queue = jackrabbit(process.env.CLOUDAMQP_URL || 'amqp://localhost')
http.globalAgent.maxSockets = Infinity;
var start = function() {
queue.on('connected', function() {
queue.create('myJob', { prefetch: 5 }, function() {
queue.handle('myJob', function(data, ack) {
console.log(data)
console.log("Job completed!")
ack()
})
})
})
}
throng(start, {
workers: 1,
lifetime: Infinity,
grace: 4000
})
Now I want a situation where I can push data to a job from the web application. So I in a middleware function I have,
// middleware.js
var jackrabbit = require('jackrabbit')
var queue = jackrabbit(process.env.CLOUDAMQP_URL || 'amqp://localhost')
app.get('/example', function(req, res, next) {
queue.publish('myJob', { data: 'my_data' })
res.send(200, { data: 'my_data' })
})
On development, I start worker.js and app.js in separate terminal windows. When I call the /example method, I expect to see the worker terminal window show the relevant console logs. However, the request just hangs. The same thing happens on Heroku.
I feel like there is something really fundamental I am missing here in terms of my understanding of jackrabbit and ampq, and how to set up a job queuing system using that.
Any help on this would be greatly appreciated as this is obviously new to me.