Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Execute job in a domain and catch uncatched exception to mark job as failed #403

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 53 additions & 43 deletions lib/queue/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,53 +151,63 @@ Worker.prototype.failed = function (job, err, fn) {
Worker.prototype.process = function (job, fn) {
var self = this
, start = new Date();
job.active( function(){
fn(
job,
function (err, result) {
if (err) {
return self.failed(job, err, fn);
}
job.set('duration', job.duration = new Date - start);
if( result ) {
try{
job.result = result;
job.set('result', JSON.stringify(result));
}catch(e){
job.set('result', JSON.stringify({error: true, message:'Invalid JSON Result: "' + result + '"' }));
var domain = require('domain').create();
domain.on('error', function(err){
self.failed(job, err, fn);
});

domain.run(function() {
job.active(function () {
fn(
job,
function (err, result) {
if (err) {
return self.failed(job, err, fn);
}
}
job.complete( function(){
job.attempt( function(){
self.emit('job complete', job);
events.emit(job.id, 'complete', result);
if( job.removeOnComplete() ) {
job.remove();
job.set('duration', job.duration = new Date - start);
if (result) {
try {
job.result = result;
job.set('result', JSON.stringify(result));
} catch (e) {
job.set('result', JSON.stringify({
error: true,
message: 'Invalid JSON Result: "' + result + '"'
}));
}
}
job.complete(function () {
job.attempt(function () {
self.emit('job complete', job);
events.emit(job.id, 'complete', result);
if (job.removeOnComplete()) {
job.remove();
}
});
}.bind(this));
self.start(fn);
}, {
/**
* @author behrad
* @pause: let the processor to tell worker not to continue processing new jobs
*/
pause: function (fn, timeout) {
timeout = timeout || 5000;
self.queue.shutdown(fn, Number(timeout), self.type);
},
/**
* @author behrad
* @pause: let the processor to trigger restart for they job processing
*/
resume: function () {
if (self.resume()) {
self.start(fn);
}
});
}.bind(this));
self.start(fn);
},{
/**
* @author behrad
* @pause: let the processor to tell worker not to continue processing new jobs
*/
pause: function( fn, timeout ){
timeout = timeout || 5000;
self.queue.shutdown( fn, Number(timeout), self.type);
},
/**
* @author behrad
* @pause: let the processor to trigger restart for they job processing
*/
resume: function () {
if (self.resume()) {
self.start(fn);
}
}
}
);
}.bind(this));
);
}.bind(this));
});
return this;
};

Expand Down
19 changes: 19 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,25 @@ describe('Jobs', function () {
});
});

it('should catch uncatched exception and mark job as failed', function(testDone) {
var catched = false;
jobs.create('failedJob', {}).on('complete', function() {
throw new Error('Job should be marked as failed and not complete');
}).on('failed', function() {
catched.should.be.equal(true);
testDone();
}).save();

jobs.process('failedJob', 1, function() {
try {
throw new Error('this should be catched')
} catch (err) {
catched = true;
}
throw new Error('toto');
});
});

it('should retry on failure if attempts is set', function (testDone) {
var job = jobs.create('failure-attempts', {});
var failures = 0;
Expand Down