From 5edcbfacb7101f282ea1dee3e02b24e8e8bfc331 Mon Sep 17 00:00:00 2001 From: HugoPoi Date: Sat, 10 Aug 2019 19:21:39 +0200 Subject: [PATCH] feat: add immediately option for every repeatable job --- REFERENCE.md | 1 + lib/repeatable.js | 8 +++++-- test/test_repeat.js | 55 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-) diff --git a/REFERENCE.md b/REFERENCE.md index aab5c1644..b03d8f213 100644 --- a/REFERENCE.md +++ b/REFERENCE.md @@ -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. } ``` diff --git a/lib/repeatable.js b/lib/repeatable.js index 6a3c3400a..7a682b0a0 100644 --- a/lib/repeatable.js +++ b/lib/repeatable.js @@ -58,7 +58,8 @@ module.exports = function(Queue) { _.defaultsDeep( { repeat: { - count: currentCount + count: currentCount, + immediately: false, }, jobId: customId, delay: delay < 0 ? 0 : delay, @@ -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 = diff --git a/test/test_repeat.js b/test/test_repeat.js index 3bd085f17..185da2c40 100644 --- a/test/test_repeat.js +++ b/test/test_repeat.js @@ -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' }); @@ -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' @@ -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');