Skip to content

Instantly share code, notes, and snippets.

@oakinogundeji
Created April 15, 2016 12:02
Show Gist options
  • Save oakinogundeji/0de949302286f8661c90fea55f2e8a90 to your computer and use it in GitHub Desktop.
Save oakinogundeji/0de949302286f8661c90fea55f2e8a90 to your computer and use it in GitHub Desktop.
Implementing kue as a job queueing solution
/**
*Module dependencies
*/
//-----------------------------------------------------------------------------
var
kue = require('kue'),
config = require('../config/config');
//=============================================================================
/**
*Create 'queue' instance
*/
//-----------------------------------------------------------------------------
var queue = kue.createQueue({
redis: config.redisConfig,
jobEvents: false
});
//=============================================================================
/**
*Module helpers
*/
//-----------------------------------------------------------------------------
function processJob(name, req, res) {
queue.process(name, 10, function (job, done) {//'10' sets the job processing concurrency
console.log('Now processing job with id: %d and type: %s', job.id, name);
console.log('The job title is: ', job.data.title);
var duration = job.data.title.split('').length;
console.log('duration for the job with %d is %d seconds', job.id, duration);
setTimeout(function () {//simulating a long running process
return done();//call done() when finished processing jobs
}, 1000 * duration)
});
}
//=============================================================================
/**
*Configure processor
*/
//-----------------------------------------------------------------------------
queue.watchStuckJobs(5000);//watch for any stuck jobs for 5seconds before
//processing other jobs in the queue
queue.
on('job complete', function (id, result) {
console.log('The job with id: %d has been completed with result: %s', id, result);
}).
on('job failed', function (err) {
console.error('There was an error processing the job with id: %d', id);
console.error(err);
console.error(err.stacktrace);
});
//=============================================================================
/**
*Export module helper
*/
//-----------------------------------------------------------------------------
module.exports = processJob;
//=============================================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment