Created
August 31, 2014 18:50
-
-
Save corbanb/ac5606ab11fb38e614f0 to your computer and use it in GitHub Desktop.
Kue + RedisToGo + Heroku + Worker
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 db = require('./db'), | |
TwitterController = require('./controllers/twitter'), | |
kue = require('kue'), | |
url = require('url'); | |
var redisUrl = url.parse(process.env.REDISTOGO_URL); | |
var tc = new TwitterController(); | |
var Worker = (function() { | |
var jobs, | |
that; | |
function init(job) { | |
jobs = kue.createQueue({ | |
prefix: 'jobs', | |
redis: { | |
port: redisUrl.port, | |
host: redisUrl.hostname, | |
auth: redisUrl.auth.split(":")[1] | |
} | |
}); | |
this.job = job; | |
that = this; | |
setInterval(createJob, this.job.interval); | |
} | |
function createJob() { | |
var job = jobs.create(that.job.name) | |
.save(function(error) { | |
jobSaved(error); | |
}) | |
.on('complete', function() { | |
jobComplete(); | |
}); | |
} | |
function executeJob() { | |
that.job.fn(finishJob); | |
} | |
function finishJob() { | |
jobs.process(that.job.name, function(redisJob, done) { | |
done(); | |
}); | |
} | |
// Event Handlers | |
function jobComplete() { | |
console.log(that.job.name + ' job complete'); | |
} | |
function jobSaved(error) { | |
if (!error) { | |
console.log(that.job.name + ' job saved'); | |
executeJob(); | |
} else { | |
console.log(that.job.name + ' job failed to save', error); | |
} | |
} | |
return { | |
init: init | |
} | |
}); | |
function retweetWorker(callback) { | |
return tc.worker(callback); | |
} | |
worker = new Worker(); | |
worker.init({ | |
name: 'updateRetweetCount', | |
fn: retweetWorker, | |
interval: 15000 | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment