Skip to content

Instantly share code, notes, and snippets.

@yeukhon
Created July 24, 2014 04:08
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yeukhon/0e331524784501b134cc to your computer and use it in GitHub Desktop.
Save yeukhon/0e331524784501b134cc to your computer and use it in GitHub Desktop.
Parent and child job using kue + node.js + Q.js based on node-job-queue example (https://github.com/prateekbhatt/node-job-queue/blob/master/nested-job-queue.js)
var kue = require('kue'),
jobs = kue.createQueue();
var Q = require('q');
var db = {};
function parentJob(id, done) {
var job = jobs.create('parent', {
type: 'PARENT',
id: id
});
job
.on('complete', function() {
console.log('Job', job.id, 'of type', job.data.type, '#', job.data.id, ' is done');
done();
})
.on('failed', function() {
console.log('Job', job.id, 'of type', job.data.type, '#', job.data.id, ' has failed');
done();
})
job.save();
}
function childJob(cid, done) {
var deferred = Q.defer();
var job = jobs.create('child', {
type: 'CHILD',
cid: cid
});
job
.on('complete', function() {
console.log('Job', job.id, 'of type', job.data.type,
'#', job.data.cid, ' is done');
deferred.resolve({
done: true,
job: job.data,
success: true
});
})
.on('failed', function() {
console.log('Job', job.id, 'of type', job.data.type,
'#', job.data.cid, ' has failed');
deferred.resolve({
done: true,
job: job.data,
success: false
});
})
job.save();
return deferred.promise;
}
jobs.process('parent', function(job, done) {
/* carry out all the parent job functions here */
var promises = [
job.data.id + '-' + 1,
job.data.id + '-' + 2
].map(
function(x) {
return childJob(x, done)
});
Q.all(promises)
.then(function(res) {
console.log('all jobs for parent ', job.data.id,
' completed');
console.log(res);
done();
});
})
jobs.process('child', function(job, done) {
/* carry out all the child job functions here */
setTimeout(function() {
console.log('working on child job ' + job.data.cid);
done();
}, 2000);
})
parentJob(13, function() {});
parentJob(14, function() {});
parentJob(15, function() {});
parentJob(16, function() {});
@yeukhon
Copy link
Author

yeukhon commented Jul 24, 2014

Sample output:

working on child job 13-1
Job 350 of type CHILD # 13-1  is done
working on child job 13-2
Job 351 of type CHILD # 13-2  is done
all jobs for parent  13  completed
[ { done: true,
    job: { type: 'CHILD', cid: '13-1' },
    success: true },
  { done: true,
    job: { type: 'CHILD', cid: '13-2' },
    success: true } ]
Job 346 of type PARENT # 13  is done
working on child job 14-1
Job 352 of type CHILD # 14-1  is done
working on child job 14-2
Job 353 of type CHILD # 14-2  is done
all jobs for parent  14  completed
[ { done: true,
    job: { type: 'CHILD', cid: '14-1' },
    success: true },
  { done: true,
    job: { type: 'CHILD', cid: '14-2' },
    success: true } ]
Job 347 of type PARENT # 14  is done
working on child job 15-1
Job 354 of type CHILD # 15-1  is done
working on child job 15-2
Job 355 of type CHILD # 15-2  is done
all jobs for parent  15  completed
.....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment