feat(repeatable): store key in repeat options

This commit is contained in:
Manuel Astudillo
2021-07-16 16:09:01 +02:00
committed by GitHub
parent 57d8679cdd
commit dab0d82661
3 changed files with 56 additions and 36 deletions
+12 -1
View File
@@ -305,6 +305,7 @@ interface RepeatOpts {
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.)
count?: number; // The start value for the repeat iteration count.
readonly key: string; // The key for the repeatable job metadata in Redis.
}
```
@@ -534,7 +535,17 @@ for the job when it was added.
removeRepeatableByKey(key: string): Promise<void>
```
Removes a given repeatable job by its key.
Removes a given repeatable job by its key so that no more repeteable jobs will be processed for this
particular job.
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:
```ts
const job = await queue.add('remove', { foo: 'bar' }, { repeat });
// Store "job.opts.repeat.key" somewhere and later
await removeRepeatbleByKey(key);
```
---
+32 -33
View File
@@ -38,42 +38,41 @@ module.exports = function(Queue) {
const nextMillis = getNextMillis(now, repeat);
if (nextMillis) {
const jobId = repeat.jobId ? repeat.jobId + ':' : ':';
const repeatJobKey = getRepeatKey(name, repeat, jobId);
const repeatKey = getRepeatKey(name, repeat, jobId);
const createNextJob = () => {
return client
.zadd(this.keys.repeat, nextMillis, repeatJobKey)
.then(() => {
//
// Generate unique job id for this iteration.
//
const customId = getRepeatJobId(
name,
jobId,
nextMillis,
md5(repeatJobKey)
);
now = Date.now();
const delay = nextMillis - now;
return client.zadd(this.keys.repeat, nextMillis, repeatKey).then(() => {
//
// Generate unique job id for this iteration.
//
const customId = getRepeatJobId(
name,
jobId,
nextMillis,
md5(repeatKey)
);
now = Date.now();
const delay = nextMillis - now;
return Job.create(
this,
name,
data,
_.defaultsDeep(
{
repeat: {
count: currentCount
},
jobId: customId,
delay: delay < 0 ? 0 : delay,
timestamp: now,
prevMillis: nextMillis
return Job.create(
this,
name,
data,
_.defaultsDeep(
{
repeat: {
count: currentCount,
key: repeatKey
},
opts
)
);
});
jobId: customId,
delay: delay < 0 ? 0 : delay,
timestamp: now,
prevMillis: nextMillis
},
opts
)
);
});
};
if (skipCheckExists) {
@@ -83,7 +82,7 @@ module.exports = function(Queue) {
// Check that the repeatable job hasn't been removed
// TODO: a lua script would be better here
return client
.zscore(this.keys.repeat, repeatJobKey)
.zscore(this.keys.repeat, repeatKey)
.then(repeatableExists => {
// The job could have been deleted since this check
if (repeatableExists) {
+12 -2
View File
@@ -75,7 +75,8 @@ describe('repeat', () => {
expect(job1.opts.repeat).to.be.deep.equal({
count: 1,
cron: '0 * * * * *',
startDate: '2020-09-02T22:29:00Z'
startDate: '2020-09-02T22:29:00Z',
key: '__default__::::0 * * * * *'
});
const job2 = await queue.add(
@@ -96,7 +97,8 @@ describe('repeat', () => {
count: 1,
cron: '0 * * * * *',
startDate: '2020-09-02T22:29:00Z',
endDate: '2020-09-05T01:44:37Z'
endDate: '2020-09-05T01:44:37Z',
key: '__default__::1599270277000::0 * * * * *'
});
});
@@ -474,6 +476,14 @@ describe('repeat', () => {
expect(delayedJobs).to.have.length(0);
});
it('should return repeatable job key', async () => {
const repeat = { cron: '*/2 * * * * *' };
const job = await queue.add('remove', { foo: 'bar' }, { repeat });
expect(job.opts.repeat.key).to.be.equal('remove::::*/2 * * * * *');
});
it('should be able to remove repeatable jobs by key that has a jobId', async () => {
const repeat = { cron: '*/2 * * * * *' };