Skip to content

Instantly share code, notes, and snippets.

@pulkitsinghal
Created June 2, 2017 21:45
Show Gist options
  • Save pulkitsinghal/cb34612078696b2ce14b6256bad62bcc to your computer and use it in GitHub Desktop.
Save pulkitsinghal/cb34612078696b2ce14b6256bad62bcc to your computer and use it in GitHub Desktop.
KUE - experiment w/ various ways to shutdown after processing only one payload
var kue = require('kue')
var queue = kue.createQueue({
redis: process.env.REDIS_URL
});
queue.process('crawl', function(job, ctx, done){
console.log((new Date()).toLocaleString(), 'inside queue.process("crawl") for job id:', job.id);
doSomething(job.data, ctx, done);
//exit(); // Scenario C: don't like it because shutdown can happen before the job finishes,
// the TIMEOUT is total guess work
// what i really want is for kue to run one job
// and one job only
// and then shutdown
});
// WARN: if we forget a variable in the signature and NODE crashes, the container does not shutdown in 1 out of a 1000 times
function doSomething(payload, ctx, done) {
console.log((new Date()).toLocaleString(), 'inside doSomething()');
// pretend to work and finish after 30 seconds
setTimeout(function() {
console.log('successfully ran the job');
console.log('payload', payload);
done();
// scenario A - try shut down after only one job
/*console.log((new Date()).toLocaleString(), 'WILL TRY TO PAUSE NOW...');
ctx.pause( 1000, function(err){
console.log((new Date()).toLocaleString(), 'Worker is paused...');
setTimeout( function(){
console.log( (new Date()).toLocaleString(), 'shutdown will now commence');
exit();
}, 1000 );
});*/
}, 5000);
}
function exit(){
var TIMEOUT = 1;
queue.shutdown( TIMEOUT, function(err) {
console.log( (new Date()).toLocaleString(), 'Kue shutdown', err||'' );
process.exit( 0 );
});
}
// scenario B - try shut down after only one job
setTimeout( function () {
console.log( '[ Shutting down when all jobs finish... ]' );
queue.shutdown( function ( err ) {
console.log( '[ All jobs finished. Kue is shut down. ]' );
process.exit( 0 );
} )
}, 1000 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment