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

Improvements for Queue#processStalledJobs #311

Closed
Closed
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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ listened by some other service that stores the results in a database.
## Reference

<a name="queue"/>
###Queue(queueName, redisPort, redisHost, [redisOpts])
###Queue(queueName, redisPort, redisHost, [redisOpts], [queueOpts])

This is the Queue constructor. It creates a new Queue that is persisted in
Redis. Everytime the same queue is instantiated it tries to process all the
Expand All @@ -330,6 +330,9 @@ __Arguments__
redisPort {Number} A port where redis server is running.
redisHost {String} A host specified as IP or domain where redis is running.
redisOptions {Object} Options to pass to the redis client. https://github.com/mranney/node_redis
queueOpts {Object} Options to drive the Queue behavior
queueOpts.processStalledJobs {Boolean} Automatically process potentially jobs stuck in active
state (default: true)
```

---------------------------------------
Expand Down
35 changes: 26 additions & 9 deletions lib/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var LOCK_RENEW_TIME = 5000; // 5 seconds is the renew time.
var CLIENT_CLOSE_TIMEOUT_MS = 5000;
var POLLING_INTERVAL = 5000;

var Queue = function Queue(name, redisPort, redisHost, redisOptions){
var Queue = function Queue(name, redisPort, redisHost, redisOptions, queueOptions){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs readme update?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bull's documentation is currently outdated in many regards, I'm addressing this: #309 by documenting everything in jsdoc format, if documentation lives closer to the code, it's much more likely it will be up to date. There are many helpers that generate nice looking documentation, I particularly liked this one: https://camo.githubusercontent.com/724b9224844b6b4f2cd19b3bce8d25015fa54cfa/687474703a2f2f7075752e73682f674f794e652f363663336164636239372e706e67

In any case, yes, this needs a readme update

if(!(this instanceof Queue)){
return new Queue(name, redisPort, redisHost, redisOptions);
}
Expand All @@ -57,7 +57,7 @@ var Queue = function Queue(name, redisPort, redisHost, redisOptions){
var redisOpts = opts.redis || {};
redisPort = redisOpts.port;
redisHost = redisOpts.host;
redisOptions = redisOpts.opts || {};
redisOptions = redisOpts.opts || {};
redisOptions.db = redisOpts.DB;
}

Expand All @@ -77,6 +77,13 @@ var Queue = function Queue(name, redisPort, redisHost, redisOptions){
redisPort = redisPort || 6379;
redisHost = redisHost || '127.0.0.1';

queueOptions = _.pick(queueOptions, ['processStalledJobs']);
queueOptions = _.defaults(queueOptions, {
processStalledJobs: true
});

this.opts = queueOptions;

var _this = this;

this.name = name;
Expand Down Expand Up @@ -423,7 +430,10 @@ Queue.prototype.run = function(concurrency){
var promises = [];
var _this = this;

return this.processStalledJobs().then(function(){
// In case of connection loss, running `processStalledJobs` will repick jobs here.
var start = this.opts.processStalledJobs ? this.processStalledJobs() : Promise.resolve();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this still needed to be run when the queue starts, instead of just letting it start after _this.LOCK_RENEW_TIME ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The expectations of Bull is to process them immediately on reconnection. Not calling it to begin with result in many failures across tests.


return start.then(function(){

while(concurrency--){
promises.push(new Promise(_this.processJobs));
Expand All @@ -433,8 +443,11 @@ Queue.prototype.run = function(concurrency){
// Set process Stalled jobs intervall
//
clearInterval(_this.stalledJobsInterval);
_this.stalledJobsInterval =
setInterval(_this.processStalledJobs, _this.LOCK_RENEW_TIME);
if(_this.opts.processStalledJobs) {
_this.stalledJobsInterval =
setInterval(_this.processStalledJobs, _this.LOCK_RENEW_TIME);

}

return Promise.all(promises);
});
Expand Down Expand Up @@ -475,15 +488,19 @@ Queue.prototype.updateDelayTimer = function(newDelayedTimestamp){
};

/**
Process jobs that have been added to the active list but are not being
processed properly.
* Process jobs that have been added to the active list but are not being
* processed properly.
*
* @param {Number?} limit Only process this many number of jobs. Greater than 1, otherwise -1
*/
Queue.prototype.processStalledJobs = function(){
Queue.prototype.processStalledJobs = function(limit){
var _this = this;
limit = limit > 0 ? limit - 1 : -1;

if(this.closing){
return this.closing;
} else{
return this.client.lrangeAsync(this.toKey('active'), 0, -1).then(function(jobs){
return this.client.lrangeAsync(this.toKey('active'), 0, limit).then(function(jobs){
return Promise.each(jobs, function(jobId) {
return Job.fromId(_this, jobId).then(_this.processStalledJob);
});
Expand Down
172 changes: 172 additions & 0 deletions test/test_queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,178 @@ describe('Queue', function () {
}).catch(done);
});

it('only process a limited amount of stalled jobs', function(done) {
this.timeout(12000);

var queue2 = utils.buildQueue('limited-stalled-job-processing' + uuid(), {
processStalledJobs: false
});

var completed = _.after(5, function(){
queue2.removeListener('completed', completed);
var client = redis.createClient();
var movingPromises = [];
// This simulates all 5 jobs to be in a stalled state.
for(var i = 1; i <= 5; i++) {
movingPromises.push(client.multi()
.srem(queue2.toKey('completed'), i)
.lpush(queue2.toKey('active'), i)
.execAsync());
}

Promise.all(movingPromises).then(function() {
return queue2.getActiveCount().then(function(count) {
expect(count).to.equal(5);
});
}).then(function() {
return queue2.processStalledJobs(3);
}).then(function() {
return queue2.getActiveCount().then(function(count) {
expect(count).to.equal(2);
});
}).then(function() {
return queue2.getCompletedCount().then(function(count) {
expect(count).to.equal(3);
});
}).catch(function(err) {
expect(err).to.be(null);
}).finally(function() {
queue2.close(true).then(done);
});
});

queue2.on('completed', completed);

for(var i = 1; i <= 5; i++) {
queue2.add({ foo: 'bar' });
}

queue2.process(function (job, jobDone) {
expect(job.data.foo).to.be.equal('bar');
jobDone();
});
});

it('should not process stalled jobs if disabled', function(done) {
this.timeout(12000);

var queue2 = utils.buildQueue('limited-stalled-job-processing' + uuid(), {
processStalledJobs: false
});

queue2.LOCK_RENEW_TIME = 100;

var completed = _.after(5, function(){
queue2.removeListener('completed', completed);
var client = redis.createClient();
var movingPromises = [];
// This simulates all 5 jobs to be in a stalled state.
for(var i = 1; i <= 5; i++) {
movingPromises.push(client.multi()
.srem(queue2.toKey('completed'), i)
.lpush(queue2.toKey('active'), i)
.execAsync());
}

Promise.all(movingPromises).then(function() {
return queue2.getActiveCount().then(function(count) {
expect(count).to.equal(5);
});
}).delay(200).then(function() {
return queue2.getActiveCount().then(function(count) {
expect(count).to.equal(5);
});
}).catch(function(err) {
expect(err).to.be(null);
}).finally(function() {
queue2.close(true).then(done);
});
});

queue2.on('completed', completed);

for(var i = 1; i <= 5; i++) {
queue2.add({ foo: 'bar' });
}

queue2.process(function (job, jobDone) {
expect(job.data.foo).to.be.equal('bar');
jobDone();
});
});

it('should not process stalled jobs on a reconnection', function(done) {
this.timeout(12000);
var queueName = 'dont-process-stalled-jobs-on-reconnection' + uuid();

var queue2 = utils.buildQueue(queueName, {
processStalledJobs: false
});

var queue3;

var jobHandler = function(job, jobDone) {
jobDone();
};

queue2.LOCK_RENEW_TIME = 30;

var completed = _.after(5, function(){
queue2.removeListener('completed', completed);
var client = redis.createClient();
var movingPromises = [];
// This simulates all 5 jobs to be in a stalled state.
for(var i = 1; i <= 5; i++) {
movingPromises.push(client.multi()
.srem(queue2.toKey('completed'), i)
.lpush(queue2.toKey('active'), i)
.execAsync());
}

Promise.all(movingPromises).then(function() {
return queue2.getActiveCount().then(function(count) {
expect(count).to.equal(5);
});
}).delay(100).then(function() {
return queue2.getActiveCount().then(function(count) {
expect(count).to.equal(5);
});
}).then(function() {
queue3 = utils.buildQueue(queueName, {
processStalledJobs: false
});

return new Promise(function(resolve) {
queue3.on('ready', function() {
queue3.process(jobHandler);
resolve();
});
});
}).delay(100).then(function() {
return queue3.getActiveCount().then(function(count) {
expect(count).to.equal(5);
});
}).catch(function(err) {
expect(err).to.be(null);
}).finally(function() {
var closing = [queue2.close(true)];
if (queue3) closing.push(queue3.close(true));

Promise.all(closing).then(function() {
done();
});
});
});

queue2.on('completed', completed);

for(var i = 1; i <= 5; i++) {
queue2.add({ foo: 'bar' });
}

queue2.process(jobHandler);
});

it('process a job that fails', function (done) {
var jobError = new Error('Job Failed');

Expand Down
4 changes: 2 additions & 2 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ function simulateDisconnect(queue){
queue.eclient.stream.end();
}

function buildQueue(name) {
var queue = new Queue(name || STD_QUEUE_NAME, 6379, '127.0.0.1');
function buildQueue(name, queueOptions) {
var queue = new Queue(name || STD_QUEUE_NAME, 6379, '127.0.0.1', {}, queueOptions);
queues.push(queue);
return queue;
}
Expand Down