Created
June 2, 2017 21:51
-
-
Save pulkitsinghal/a38e2321643f1b103aa49c27755d0805 to your computer and use it in GitHub Desktop.
KUE - sure fire way to shutdown after processing only one payload
This file contains hidden or 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') | |
var queue = kue.createQueue({ | |
redis: process.env.REDIS_URL | |
}); | |
var JOB_NAME = 'crawl'; | |
queue.process(JOB_NAME, function(job, ctx, done){ | |
console.log((new Date()).toLocaleString(), 'inside queue.process("'+JOB_NAME+'") for job id:', job.id); | |
doSomething(job.data, ctx, done); | |
}); | |
// 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()'); | |
setTimeout(function() { // pretend to work and finish after few seconds | |
console.log('successfully ran the job'); | |
console.log('payload', payload); | |
done(); | |
}, 5000); | |
} | |
// Shut down after processing only one job | |
setTimeout( function () { | |
console.log( (new Date()).toLocaleString(), '[ Shutting down when all jobs finish... ]' ); | |
queue.shutdown( function ( err ) { | |
console.log( (new Date()).toLocaleString(), '[ 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