Created
April 15, 2016 11:49
-
-
Save oakinogundeji/b0da86939a7b45853f6c8f95a1a2c7dc to your computer and use it in GitHub Desktop.
Implementing monq as a job queueing solution
This file contains 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
/** | |
*module dependencies | |
*/ | |
//----------------------------------------------------------------------------- | |
var client = require('./producers').client; | |
//============================================================================= | |
/** | |
*create job processor instance | |
*/ | |
//----------------------------------------------------------------------------- | |
var worker = client.worker(['default']);//bind this worker to process jobs from | |
//the supplied queue(s) in this case the 'default' queue | |
//============================================================================= | |
/** | |
*Define worker helper function | |
*/ | |
//----------------------------------------------------------------------------- | |
function makeWorker() { | |
var delay;//NB just using delay to simulate I/O lag | |
worker.register({//register handler for the 'testing' job defined by the producer | |
//see line 26 of processors.js | |
testing: function (data, cb) { | |
console.log('the job with id %s is being processed by the server with processID: %s', | |
data.id, process.pid); | |
delay = data.title.split('').length; | |
console.log('Job will be delayed for %d secs', delay); | |
return setTimeout(function () { | |
return cb(null, data);//calling the 'cb' completes the job processing | |
}, 1000 * delay); | |
} | |
}); | |
worker.//bind handlers to various events emitted by the 'worker' object | |
on('complete', function (data) { | |
console.log('The queued job with id: %s has been completed by server with processID: %s', | |
data.params.id, process.pid); | |
//the line below allows us to examine the data object exposed upon completion of a job | |
console.log('The data object exposed on completion of a job: ', JSON.stringify(data)); | |
}). | |
on('failed', function (data) { | |
console.log('The queued job with id: %s couln\'t be processesd', data.params.id); | |
}). | |
on('error', function (err) { | |
console.error('There was an error spinning up the worker ', err); | |
}); | |
return worker.start();//kicks off the worker and starts processing of a job as | |
//defined by the code in the handler bound to the 'testing' job | |
} | |
//============================================================================= | |
/** | |
*export Module | |
*/ | |
//----------------------------------------------------------------------------- | |
module.exports = makeWorker; | |
//============================================================================= |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment