Permalink
Browse files

Log error messages of failed jobs to volatile list

  • Loading branch information...
1 parent 86da525 commit 1c4507e9c452be845ddb483bac3599b13e197712 @TheDeveloper TheDeveloper committed Oct 12, 2012
Showing with 44 additions and 4 deletions.
  1. +2 −0 config/default.js
  2. +14 −3 lib/worker.js
  3. +28 −1 test/queue.js
View
2 config/default.js
@@ -7,6 +7,8 @@ module.exports = {
keys: {
prefix: 'cv:',
+ logTTL: 86400,
+
queued: 'queued',
processing: 'processing',
failed: 'failed'
View
17 lib/worker.js
@@ -58,16 +58,27 @@ Worker.prototype.processed = function(err, cb) {
});
};
-Worker.prototype.fail = function(err, cb) {
+Worker.prototype.fail = function(jobError, cb) {
var self = this;
+ if(!cb)
+ cb = function(){};
// Job has failed, remove it from processing and add it to failed
this.notProcessing(function(err){
self.client.zincrby(helpers.key(self.queue.name+':failed'), 1, self.job.id, function(err, res){
if(err)
return debug(err);
- if(cb)
- cb();
+ self.logError(jobError, cb);
});
});
+};
+
+Worker.prototype.logError = function(message, cb) {
+ var now = helpers.time();
+ var dayStart = now - (now % 86400);
+ var key = helpers.key(this.queue.name+':errorLog.'+dayStart);
+ this.client.multi()
+ .lpush(key, message)
+ .expire(key, 86400)
+ .exec(cb);
};
View
29 test/queue.js
@@ -79,6 +79,7 @@ describe('Processing jobs', function(){
it('removes job from queued list', function(done){
client.lrange(helpers.key(q.name+':queued'), 0, -1, function(err, list){
should.not.exist(err);
+ should.exist(list);
list.should.not.include(''+job.id);
done();
});
@@ -93,8 +94,10 @@ describe('Processing jobs', function(){
});
});
+ var errorMsg = 'holy crickets Watman, what happened?';
+
it('places job in fail list if callback invoked with error', function(done){
- processed('ohnoes, something went wrong', function(){
+ processed(errorMsg, function(){
client.zscore(helpers.key(q.name+':failed'), job.id, function(err, numFails){
should.not.exist(err);
should.exist(numFails);
@@ -103,6 +106,30 @@ describe('Processing jobs', function(){
});
});
});
+
+ it('failed jobs with error message should get logged', function(done){
+ var now = helpers.time();
+ var dayStart = now - (now % 86400);
+ var key = helpers.key(q.name+':errorLog.'+dayStart);
+ client.lrange(key, 0, -1, function(err, log){
+ should.not.exist(err);
+ should.exist(log);
+ log.should.include(errorMsg);
+ done();
+ });
+ });
+
+ it('failed log should have ttl', function(done){
+ var now = helpers.time();
+ var dayStart = now - (now % 86400);
+ var key = helpers.key(q.name+':errorLog.'+dayStart);
+ client.ttl(key, function(err, ttl){
+ should.not.exist(err);
+ should.exist(ttl);
+ ttl.should.be.within(0, config.keys.logTTL);
+ done();
+ });
+ });
});
describe('When multiple convoys process the same queue', function(){

0 comments on commit 1c4507e

Please sign in to comment.