Created
December 16, 2011 11:39
-
-
Save hakobera/1485748 to your computer and use it in GitHub Desktop.
Kue on Heroku
This file contains 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 kue = require('kue'), | |
redis = require('redis'), | |
url = require('url'); | |
kue.redis.createClient = function() { | |
var options = { host: 'localhost' }; | |
if (process.env.REDISTOGO_URL) { | |
var redisUri = url.parse(process.env.REDISTOGO_URL); | |
options = { | |
host: redisUri.hostname, | |
port: redisUri.port, | |
pass: redisUri.auth.split(':')[1] | |
}; | |
} | |
var client = redis.createClient(options.port, options.host, options); | |
client.on("error", function (err) { | |
console.error(err); | |
}); | |
if (options.pass) { | |
client.auth(options.pass, function(err) { | |
if (err) { | |
console.error(err); | |
throw err; | |
} | |
}); | |
} | |
return client; | |
}; | |
var jobs = kue.createQueue(); | |
function create() { | |
var name = ['tobi', 'loki', 'jane', 'manny'][Math.random() * 4 | 0]; | |
jobs.create('video conversion', { | |
title: 'converting ' + name + '\'s to avi' | |
, user: 1 | |
, frames: 200 | |
}).save(); | |
setTimeout(create, Math.random() * 3000 | 0); | |
} | |
create(); | |
function create2() { | |
var name = ['tobi', 'loki', 'jane', 'manny'][Math.random() * 4 | 0]; | |
jobs.create('email', { | |
title: 'emailing ' + name + '' | |
, body: 'hello' | |
}).save(); | |
setTimeout(create2, Math.random() * 1000 | 0); | |
} | |
create2(); | |
// process video conversion jobs, 3 at a time. | |
jobs.process('video conversion', 2, function(job, done){ | |
console.log('video'); | |
setTimeout(done, Math.random() * 5000); | |
}); | |
// process 10 emails at a time | |
jobs.process('email', 10, function(job, done){ | |
console.log('email'); | |
setTimeout(done, Math.random() * 2000); | |
}); | |
kue.app.listen(process.env.PORT || 3000); | |
console.log('UI started on port ' + kue.app.address().port); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment