Skip to content

Instantly share code, notes, and snippets.

@RandomEtc
Created September 26, 2011 15:59
Show Gist options
  • Save RandomEtc/1242576 to your computer and use it in GitHub Desktop.
Save RandomEtc/1242576 to your computer and use it in GitHub Desktop.
Getting Kue working on Heroku

Workers can be activated with heroku scale:

heroku scale web=1 worker=1

Each process is $0.05/hr (or about $35/month).

You'll need a Redis instance. For RedisToGo you need a mini or higher to accept two connections AFAICS. Not sure what the upper limit is on workers yet.

heroku addons:add redistogo:mini   

A mini instance is just $5 a month and should be billed by the second. To remove, do:

heroku addons:remove redistogo   
var express = require('express')
, kue = require('kue')
, url = require('url')
, redis = require('kue/node_modules/redis');
// make sure we use the Heroku Redis To Go URL
// (put REDISTOGO_URL=redis://localhost:6379 in .env for local testing)
kue.redis.createClient = function() {
var redisUrl = url.parse(process.env.REDISTOGO_URL)
, client = redis.createClient(redisUrl.port, redisUrl.hostname);
if (redisUrl.auth) {
client.auth(redisUrl.auth.split(":")[1]);
}
return client;
};
// then access the current Queue
var jobs = kue.createQueue()
, app = express.createServer();
app.get('/', function(req, res) {
var job = jobs.create('crawl', {
url: 'http://example.com'
, token: 'foo'
});
job.on('complete', function(){
res.send("Job complete");
}).on('failed', function(){
res.send("Job failed");
}).on('progress', function(progress){
console.log('job #' + job.id + ' ' + progress + '% complete');
});
job.save();
});
// wire up Kue (see /active for queue interface)
app.use(kue.app);
app.listen(process.env.PORT);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
var kue = require('kue')
, url = require('url')
, redis = require('kue/node_modules/redis');
// make sure we use the Heroku Redis To Go URL
// (put REDISTOGO_URL=redis://localhost:6379 in .env for local testing)
kue.redis.createClient = function() {
var redisUrl = url.parse(process.env.REDISTOGO_URL)
, client = redis.createClient(redisUrl.port, redisUrl.hostname);
if (redisUrl.auth) {
client.auth(redisUrl.auth.split(":")[1]);
}
return client;
};
var jobs = kue.createQueue();
// see https://github.com/learnBoost/kue/ for how to do more than one job at a time
jobs.process('crawl', function(job, done) {
console.log(job.data);
done();
});
{
"name": "kue-test"
, "version": "0.0.1"
, "private": true
, "dependencies": {
"express": "~2.4.6"
, "ejs": "~0.0.1"
, "kue": "~0.3.1"
}
}
web: node app.js
worker: node consumer.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment