Skip to content

Commit dab0d82

Browse files
authored
feat(repeatable): store key in repeat options
1 parent 57d8679 commit dab0d82

File tree

3 files changed

+57
-37
lines changed

3 files changed

+57
-37
lines changed

REFERENCE.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ interface RepeatOpts {
305305
limit?: number; // Number of times the job should repeat at max.
306306
every?: number; // Repeat every millis (cron setting cannot be used together with this setting.)
307307
count?: number; // The start value for the repeat iteration count.
308+
readonly key: string; // The key for the repeatable job metadata in Redis.
308309
}
309310
```
310311

@@ -534,7 +535,17 @@ for the job when it was added.
534535
removeRepeatableByKey(key: string): Promise<void>
535536
```
536537

537-
Removes a given repeatable job by its key.
538+
Removes a given repeatable job by its key so that no more repeteable jobs will be processed for this
539+
particular job.
540+
There are currently two ways to get the "key" of a repeatable job, either listing alll the existing repeatable jobs, and getting the "key" for the one you want to delete, or by getting it from the added job, like this:
541+
542+
```ts
543+
const job = await queue.add('remove', { foo: 'bar' }, { repeat });
544+
545+
// Store "job.opts.repeat.key" somewhere and later
546+
547+
await removeRepeatbleByKey(key);
548+
```
538549

539550
---
540551

lib/repeatable.js

+33-34
Original file line numberDiff line numberDiff line change
@@ -38,42 +38,41 @@ module.exports = function(Queue) {
3838
const nextMillis = getNextMillis(now, repeat);
3939
if (nextMillis) {
4040
const jobId = repeat.jobId ? repeat.jobId + ':' : ':';
41-
const repeatJobKey = getRepeatKey(name, repeat, jobId);
41+
const repeatKey = getRepeatKey(name, repeat, jobId);
4242

4343
const createNextJob = () => {
44-
return client
45-
.zadd(this.keys.repeat, nextMillis, repeatJobKey)
46-
.then(() => {
47-
//
48-
// Generate unique job id for this iteration.
49-
//
50-
const customId = getRepeatJobId(
51-
name,
52-
jobId,
53-
nextMillis,
54-
md5(repeatJobKey)
55-
);
56-
now = Date.now();
57-
const delay = nextMillis - now;
58-
59-
return Job.create(
60-
this,
61-
name,
62-
data,
63-
_.defaultsDeep(
64-
{
65-
repeat: {
66-
count: currentCount
67-
},
68-
jobId: customId,
69-
delay: delay < 0 ? 0 : delay,
70-
timestamp: now,
71-
prevMillis: nextMillis
44+
return client.zadd(this.keys.repeat, nextMillis, repeatKey).then(() => {
45+
//
46+
// Generate unique job id for this iteration.
47+
//
48+
const customId = getRepeatJobId(
49+
name,
50+
jobId,
51+
nextMillis,
52+
md5(repeatKey)
53+
);
54+
now = Date.now();
55+
const delay = nextMillis - now;
56+
57+
return Job.create(
58+
this,
59+
name,
60+
data,
61+
_.defaultsDeep(
62+
{
63+
repeat: {
64+
count: currentCount,
65+
key: repeatKey
7266
},
73-
opts
74-
)
75-
);
76-
});
67+
jobId: customId,
68+
delay: delay < 0 ? 0 : delay,
69+
timestamp: now,
70+
prevMillis: nextMillis
71+
},
72+
opts
73+
)
74+
);
75+
});
7776
};
7877

7978
if (skipCheckExists) {
@@ -83,7 +82,7 @@ module.exports = function(Queue) {
8382
// Check that the repeatable job hasn't been removed
8483
// TODO: a lua script would be better here
8584
return client
86-
.zscore(this.keys.repeat, repeatJobKey)
85+
.zscore(this.keys.repeat, repeatKey)
8786
.then(repeatableExists => {
8887
// The job could have been deleted since this check
8988
if (repeatableExists) {

test/test_repeat.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ describe('repeat', () => {
7575
expect(job1.opts.repeat).to.be.deep.equal({
7676
count: 1,
7777
cron: '0 * * * * *',
78-
startDate: '2020-09-02T22:29:00Z'
78+
startDate: '2020-09-02T22:29:00Z',
79+
key: '__default__::::0 * * * * *'
7980
});
8081

8182
const job2 = await queue.add(
@@ -96,7 +97,8 @@ describe('repeat', () => {
9697
count: 1,
9798
cron: '0 * * * * *',
9899
startDate: '2020-09-02T22:29:00Z',
99-
endDate: '2020-09-05T01:44:37Z'
100+
endDate: '2020-09-05T01:44:37Z',
101+
key: '__default__::1599270277000::0 * * * * *'
100102
});
101103
});
102104

@@ -474,6 +476,14 @@ describe('repeat', () => {
474476
expect(delayedJobs).to.have.length(0);
475477
});
476478

479+
it('should return repeatable job key', async () => {
480+
const repeat = { cron: '*/2 * * * * *' };
481+
482+
const job = await queue.add('remove', { foo: 'bar' }, { repeat });
483+
484+
expect(job.opts.repeat.key).to.be.equal('remove::::*/2 * * * * *');
485+
});
486+
477487
it('should be able to remove repeatable jobs by key that has a jobId', async () => {
478488
const repeat = { cron: '*/2 * * * * *' };
479489

0 commit comments

Comments
 (0)