Skip to content

Commit

Permalink
fixed Repeatable jobs failed to end #766
Browse files Browse the repository at this point in the history
  • Loading branch information
manast committed Nov 5, 2017
1 parent b28ed43 commit 62dd132
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 13 deletions.
16 changes: 6 additions & 10 deletions lib/repeatable.js
Expand Up @@ -52,21 +52,17 @@ module.exports = function(Queue){

Queue.prototype.removeRepeatable = function(name, repeat){
var _this = this;
var repeatJobKey;


if(typeof name !== 'string'){
repeat = name;
name = Job.DEFAULT_JOB_NAME;
} else if ( arguments.length == 1){
repeatJobKey = name;
}
}

return this.isReady().then(function(){
if(!repeatJobKey){
var jobId = repeat.jobId ? repeat.jobId + ':' : ':';
var repeatJobId = getRepeatJobId(name, jobId, '');
repeatJobKey = getRepeatKey(name, repeat, jobId);
}
var jobId = repeat.jobId ? repeat.jobId + ':' : ':';
var repeatJobKey = getRepeatKey(name, repeat, jobId);
var repeatJobId = getRepeatJobId(name, jobId, '', md5(repeatJobKey));

return _this.client.removeRepeatable(_this.keys.repeat, _this.keys.delayed, repeatJobId, repeatJobKey);
});
};
Expand Down
50 changes: 47 additions & 3 deletions test/test_repeat.js
Expand Up @@ -6,6 +6,7 @@ var utils = require('./utils');
var sinon = require('sinon');
var redis = require('ioredis');
var moment = require('moment');
var _ = require('lodash');

var ONE_SECOND = 1000;
var ONE_MINUTE = 60 * ONE_SECOND;
Expand Down Expand Up @@ -210,11 +211,54 @@ describe('repeat', function () {
counter ++;
if(counter == 20){
return queue.removeRepeatable('remove', repeat).then(function(){
setTimeout(done, ONE_SECOND);
_this.clock.tick(nextTick);
return queue.getDelayed().then(function(delayed){
expect(delayed).to.be.empty;
done();
return null;
});
});
} else if (counter > 20){
done(Error('should not repeat more than 20 times'));
}
});

var prev;
var counter = 0;
queue.on('completed', function(job){
_this.clock.tick(nextTick);
if(prev){
expect(prev.timestamp).to.be.lt(job.timestamp);
expect(job.timestamp - prev.timestamp).to.be.gte(2000);
}
prev = job;
});
});

it('should allow removing a customId repeatable job', function(done){
var _this = this;
var date = new Date('2017-02-07 9:24:00');
this.clock.tick(date.getTime());

var nextTick = 2 * ONE_SECOND;
var repeat = {cron: '*/2 * * * * *'};

queue.add({foo: 'bar'}, {repeat: repeat, jobId : 'xxxx'}).then(function(){
_this.clock.tick(nextTick);
});

queue.process(function(){
counter ++;
if(counter == 20){
return queue.removeRepeatable(_.defaults({jobId:'xxxx'}, repeat)).then(function(){
_this.clock.tick(nextTick);
return null;
return queue.getDelayed().then(function(delayed){
expect(delayed).to.be.empty;
done();
return null;
});
});
} if (counter > 20){
} else if (counter > 20){
done(Error('should not repeat more than 20 times'));
}
});
Expand Down

0 comments on commit 62dd132

Please sign in to comment.