Skip to content

Instantly share code, notes, and snippets.

@oakinogundeji
Created April 15, 2016 12:01
Show Gist options
  • Save oakinogundeji/b6fe98419c2ef2795d1c29f8a1752dd7 to your computer and use it in GitHub Desktop.
Save oakinogundeji/b6fe98419c2ef2795d1c29f8a1752dd7 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,//allows us to use a suitable redis config for the env
jobEvents: false//this allows us to bind to 'queue' level events which are more
//resilient to application failures
});
//=============================================================================
/**
*Module helpers
*/
//-----------------------------------------------------------------------------
function newJob(name, data, req, res) {//kue allows us to dynamically define the 'type'
//of job we want to process as determined by the client
var job = queue.create(name, data);//we crate a job with name 'name' and data 'data'
job.
removeOnComplete(false).//not removing completed jobs allows us to view them in the UI
//we could either set the flag to 'true' for production or run a completed jobs
//removal script as recommended by the documentation
priority('critical').
attempts(5).
backoff({type: 'exponential'}).
save(function (err) {
if(err) {
console.error('There was an error saving the job with id: %d to the main Queue', id);
console.error(err);
return;
}
else {
req.app.emit('newJob', name);//emitting events via the express app instance
//allows us to loosely couple our app
return res.status(200).json('Processing the job!');//close client req connection
}
});
/*job.//we don't use job events because we set the flag to false
on('complete', function (result) {
console.log('The job with id: %d has been completed with result: %s', job.id, result);
}).
on('failed', function (err) {
console.error('There was an error completeing the job with id: %d ', job.id);
console.error(err);
});*/
}
//=============================================================================
/**
*Bind handlers for queue events
*/
//-----------------------------------------------------------------------------
queue.
on('job enqueue', function (id, type) {
console.log('The job with id: %d and type: %s has been successfully queued for processing',
id, type);
}).
on('error', function (err) {
console.error('There was an error creating the main job Queue');
console.error(err);
return;
});
//=============================================================================
/**
*Export module helper
*/
//-----------------------------------------------------------------------------
module.exports = newJob;
//=============================================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment