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

feat: exactRecurrence to repeat job regardless of the interval of every #1498

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
3 changes: 2 additions & 1 deletion REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,8 @@ interface RepeatOpts {
startDate?: Date | string | number; // Start date when the repeat job should start repeating (only with cron).
endDate?: Date | string | number; // End date when the repeat job should stop repeating.
limit?: number; // Number of times the job should repeat at max.
every?: number; // Repeat every millis (cron setting cannot be used together with this setting.)
every?: number; // Repeat every milliseconds within the nearest interval of length "every" (cron setting cannot be used together with this setting.)
exactRecurrence?: boolean; // repeat job every n milliseconds regardless the interval of every (only with every.)
count?: number; // The start value for the repeat iteration count.
}
```
Expand Down
4 changes: 3 additions & 1 deletion lib/repeatable.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,9 @@ module.exports = function(Queue) {
}

if (opts.every) {
return Math.floor(millis / opts.every) * opts.every + opts.every;
return opts.exactRecurrence
? millis + opts.every
: Math.floor(millis / opts.every) * opts.every + opts.every;
}

const currentDate =
Expand Down
37 changes: 37 additions & 0 deletions test/test_repeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -745,4 +745,41 @@ describe('repeat', () => {
}
});
});

it('should repeat every 2 seconds', function(done) {
this.timeout(20000);
const _this = this;
const date = new Date('2017-02-07 9:24:00');
this.clock.tick(date.getTime());
const nextTick = 2 * ONE_SECOND + 500;

queue
.add(
'repeat',
{ foo: 'bar' },
{ repeat: { every: 2000, exactRecurrence: true } }
)
.then(() => {
_this.clock.tick(nextTick);
});

queue.process('repeat', () => {
// dummy
});

let prev;
let counter = 0;
queue.on('completed', 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;
counter++;
if (counter == 20) {
done();
}
});
});
});