From 1c76195782fd3b03789c29799ca70859ff559a59 Mon Sep 17 00:00:00 2001 From: Manuel Astudillo Date: Tue, 27 Oct 2020 09:09:30 +0100 Subject: [PATCH] fix(repeat): remove last delayed job --- .eslintrc.yml | 2 +- lib/repeatable.js | 12 +++++-- test/test_repeat.js | 86 +++++++++++++++++++++++++++++++++++++-------- 3 files changed, 82 insertions(+), 18 deletions(-) diff --git a/.eslintrc.yml b/.eslintrc.yml index 7f387ef..939528e 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -22,13 +22,13 @@ rules: no-alert: 2 no-console: [2, { allow: ['warn', 'error'] }] no-underscore-dangle: 0 + object-shorthand: 0 strict: [2, 'global'] no-var: 2 prefer-arrow-callback: 2 prefer-const: 2 no-inner-declarations: 0 - object-shorthand: [2, 'consistent-as-needed'] newline-per-chained-call: 2 mocha/no-exclusive-tests: 2 diff --git a/lib/repeatable.js b/lib/repeatable.js index 9f1cb57..6a3c340 100644 --- a/lib/repeatable.js +++ b/lib/repeatable.js @@ -113,14 +113,22 @@ module.exports = function(Queue) { }; Queue.prototype.removeRepeatableByKey = function(repeatJobKey) { - const data = this._keyToData(repeatJobKey); + const repeatMeta = this._keyToData(repeatJobKey); const queueKey = this.keys['']; + const jobId = repeatMeta.id ? repeatMeta.id + ':' : ':'; + const repeatJobId = getRepeatJobId( + repeatMeta.name || Job.DEFAULT_JOB_NAME, + jobId, + '', + md5(repeatJobKey) + ); + return this.isReady().then(() => { return this.client.removeRepeatable( this.keys.repeat, this.keys.delayed, - data.id, + repeatJobId, repeatJobKey, queueKey ); diff --git a/test/test_repeat.js b/test/test_repeat.js index fad5362..d7dacd4 100644 --- a/test/test_repeat.js +++ b/test/test_repeat.js @@ -57,6 +57,49 @@ describe('repeat', () => { .catch(done); }); + it('should add a repetable job when using stardDate and endDate', async () => { + const job1 = await queue.add( + { + name: 'job1' + }, + { + repeat: { + cron: '0 * * * * *', + startDate: '2020-09-02T22:29:00Z' + } + } + ); + + expect(job1).to.exist; + expect(job1.opts).to.have.property('repeat'); + expect(job1.opts.repeat).to.be.deep.equal({ + count: 1, + cron: '0 * * * * *', + startDate: '2020-09-02T22:29:00Z' + }); + + const job2 = await queue.add( + { + name: 'job2' + }, + { + repeat: { + cron: '0 * * * * *', + startDate: '2020-09-02T22:29:00Z', + endDate: '2020-09-05T01:44:37Z' + } + } + ); + expect(job2).to.exist; + expect(job2.opts).to.have.property('repeat'); + expect(job2.opts.repeat).to.be.deep.equal({ + count: 1, + cron: '0 * * * * *', + startDate: '2020-09-02T22:29:00Z', + endDate: '2020-09-05T01:44:37Z' + }); + }); + it('should get repeatable jobs with different cron pattern', done => { const crons = [ '10 * * * * *', @@ -414,23 +457,36 @@ describe('repeat', () => { }); }); - it('should be able to remove repeatable jobs by key', () => { + it('should be able to remove repeatable jobs by key', async () => { const repeat = { cron: '*/2 * * * * *' }; - return queue.add('remove', { foo: 'bar' }, { repeat }).then(() => { - return queue - .getRepeatableJobs() - .then(repeatableJobs => { - expect(repeatableJobs).to.have.length(1); - return queue.removeRepeatableByKey(repeatableJobs[0].key); - }) - .then(() => { - return queue.getRepeatableJobs(); - }) - .then(repeatableJobs => { - expect(repeatableJobs).to.have.length(0); - }); - }); + await queue.add('remove', { foo: 'bar' }, { repeat }); + + const repeatableJobs = await queue.getRepeatableJobs(); + expect(repeatableJobs).to.have.length(1); + await queue.removeRepeatableByKey(repeatableJobs[0].key); + + const repeatableJobs2 = await queue.getRepeatableJobs(); + expect(repeatableJobs2).to.have.length(0); + + const delayedJobs = await queue.getDelayed(); + expect(delayedJobs).to.have.length(0); + }); + + it('should be able to remove repeatable jobs by key that has a jobId', async () => { + const repeat = { cron: '*/2 * * * * *' }; + + await queue.add('remove', { foo: 'bar' }, { jobId: 'qux', repeat }); + + const repeatableJobs = await queue.getRepeatableJobs(); + expect(repeatableJobs).to.have.length(1); + await queue.removeRepeatableByKey(repeatableJobs[0].key); + + const repeatableJobs2 = await queue.getRepeatableJobs(); + expect(repeatableJobs2).to.have.length(0); + + const delayedJobs = await queue.getDelayed(); + expect(delayedJobs).to.have.length(0); }); it('should allow removing a customId repeatable job', function(done) {