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: add immediately option for every repeatable job #1422

Open
wants to merge 1 commit into
base: develop
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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