Created
June 3, 2017 18:19
-
-
Save pulkitsinghal/7b66d2dce2af8939bfc13365544ee365 to your computer and use it in GitHub Desktop.
Proof: one job per worker run
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
const os = require('os'); | |
var kue = require('kue'); | |
var queue = kue.createQueue({ | |
redis: process.env.REDIS_URL | |
}); | |
var JOB_NAME = 'crawl'; | |
var doNotCorruptGlobalNamespace = 1; | |
queue.process(JOB_NAME, function(job, ctx, done){ | |
console.log( os.hostname(), process.pid, (new Date()).toLocaleString(), 'inside queue.process("'+JOB_NAME+'") for job id:', job.id); | |
job.log( os.hostname(), process.pid, (new Date()).toLocaleString(), 'inside queue.process("'+JOB_NAME+'") for job id:', job.id); | |
doSomething(job, 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(job, ctx, done) { | |
var payload = job.data; | |
console.log( os.hostname(), process.pid, (new Date()).toLocaleString(), 'inside doSomething()'); | |
job.log( os.hostname(), process.pid, (new Date()).toLocaleString(), 'inside doSomething()'); | |
setTimeout(function() { // pretend to work and finish after few seconds | |
console.log( os.hostname(), process.pid, (new Date()).toLocaleString(), 'doNotCorruptGlobalNamespace', ++doNotCorruptGlobalNamespace ); | |
job.log( os.hostname(), process.pid, (new Date()).toLocaleString(), 'doNotCorruptGlobalNamespace', ++doNotCorruptGlobalNamespace ); | |
console.log( os.hostname(), process.pid, (new Date()).toLocaleString(), 'successfully ran the job:', job.id); | |
//console.log( os.hostname(), process.pid, (new Date()).toLocaleString(), 'payload', payload); | |
done(); | |
}, 5000); | |
} | |
// Shut down after processing only one job | |
setTimeout( function () { | |
console.log( os.hostname(), process.pid, (new Date()).toLocaleString(), '[ Shutting down when all jobs finish... ]' ); | |
queue.shutdown( function ( err ) { | |
console.log( os.hostname(), process.pid, (new Date()).toLocaleString(), '[ All jobs finished. Kue is shut down. ]' ); | |
process.exit( 0 ); | |
} ) | |
}, 1000 ); |
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
const os = require('os'); | |
var kue = require('kue'); | |
var queue = kue.createQueue({ | |
redis: process.env.REDIS_URL | |
}); | |
var JOB_NAME = 'crawl'; | |
var doNotCorruptGlobalNamespace = 1; | |
queue.process(JOB_NAME, function(job, ctx, done){ | |
console.log( os.hostname(), process.pid, (new Date()).toLocaleString(), 'inside queue.process("'+JOB_NAME+'") for job id:', job.id); | |
job.log( os.hostname(), process.pid, (new Date()).toLocaleString(), 'inside queue.process("'+JOB_NAME+'") for job id:', job.id); | |
doSomething(job, 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(job, ctx, done) { | |
var payload = job.data; | |
console.log( os.hostname(), process.pid, (new Date()).toLocaleString(), 'inside doSomething()'); | |
job.log( os.hostname(), process.pid, (new Date()).toLocaleString(), 'inside doSomething()'); | |
setTimeout(function() { // pretend to work and finish after few seconds | |
console.log( os.hostname(), process.pid, (new Date()).toLocaleString(), 'doNotCorruptGlobalNamespace', ++doNotCorruptGlobalNamespace ); | |
job.log( os.hostname(), process.pid, (new Date()).toLocaleString(), 'doNotCorruptGlobalNamespace', ++doNotCorruptGlobalNamespace ); | |
console.log( os.hostname(), process.pid, (new Date()).toLocaleString(), 'successfully ran the job:', job.id); | |
//console.log( os.hostname(), process.pid, (new Date()).toLocaleString(), 'payload', payload); | |
done(); | |
}, 5000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment