Skip to content

Commit

Permalink
fix(repeat): remove last delayed job
Browse files Browse the repository at this point in the history
  • Loading branch information
manast authored Oct 27, 2020
1 parent f63883b commit 1c76195
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions lib/repeatable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
86 changes: 71 additions & 15 deletions test/test_repeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 * * * * *',
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 1c76195

Please sign in to comment.