Skip to content

Commit

Permalink
feat: add immediately option for every repeatable job
Browse files Browse the repository at this point in the history
  • Loading branch information
HugoPoi committed Mar 20, 2021
1 parent 4177e57 commit 5edcbfa
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
1 change: 1 addition & 0 deletions REFERENCE.md
Expand Up @@ -303,6 +303,7 @@ interface RepeatOpts {
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.)
immediately?: boolean; // the repeat job should start right now ( work only with every settings)
count?: number; // The start value for the repeat iteration count.
}
```
Expand Down
8 changes: 6 additions & 2 deletions lib/repeatable.js
Expand Up @@ -58,7 +58,8 @@ module.exports = function(Queue) {
_.defaultsDeep(
{
repeat: {
count: currentCount
count: currentCount,
immediately: false,
},
jobId: customId,
delay: delay < 0 ? 0 : delay,
Expand Down Expand Up @@ -200,7 +201,10 @@ module.exports = function(Queue) {
}

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

const currentDate =
Expand Down
55 changes: 55 additions & 0 deletions test/test_repeat.js
Expand Up @@ -74,6 +74,7 @@ describe('repeat', () => {
expect(job1.opts).to.have.property('repeat');
expect(job1.opts.repeat).to.be.deep.equal({
count: 1,
immediately: false,
cron: '0 * * * * *',
startDate: '2020-09-02T22:29:00Z'
});
Expand All @@ -94,6 +95,7 @@ describe('repeat', () => {
expect(job2.opts).to.have.property('repeat');
expect(job2.opts.repeat).to.be.deep.equal({
count: 1,
immediately: false,
cron: '0 * * * * *',
startDate: '2020-09-02T22:29:00Z',
endDate: '2020-09-05T01:44:37Z'
Expand Down Expand Up @@ -310,6 +312,59 @@ describe('repeat', () => {
});
});

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

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

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

let prev;
let counter = 0;
queue.on('completed', job => {
_this.clock.tick(nextTick);
// The first time the delay will be shorter
if (prev && counter === 1) {
try {
expect(prev.timestamp).to.be.lt(job.timestamp);
expect(job.timestamp - prev.timestamp).to.be.gte(500);
} catch (err) {
done(err);
}
} else if (prev) {
try {
expect(prev.timestamp).to.be.lt(job.timestamp);
expect(job.timestamp - prev.timestamp).to.be.gte(2000);
} catch (err) {
done(err);
}
}
prev = job;
counter++;
if (counter == 20) {
done();
}
});
});

it('should repeat once a day for 5 days', function(done) {
const _this = this;
const date = new Date('2017-05-05 13:12:00');
Expand Down

0 comments on commit 5edcbfa

Please sign in to comment.