Skip to content

Commit

Permalink
feat(remove-repeatable): return boolean depending on job existence (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
roggervalf committed May 11, 2022
1 parent a265398 commit 59b0da7
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 15 deletions.
6 changes: 4 additions & 2 deletions src/classes/compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,11 @@ export class Queue3<T = any> extends EventEmitter {
*
* name: The name of the to be removed job
*/
async removeRepeatable(name: string, opts: RepeatOptions): Promise<void> {
async removeRepeatable(name: string, opts: RepeatOptions): Promise<boolean> {
const repeat = await this.queue.repeat;
return repeat.removeRepeatable(name, opts, opts.jobId);
const removed = await repeat.removeRepeatable(name, opts, opts.jobId);

return !removed;
}

/**
Expand Down
14 changes: 10 additions & 4 deletions src/classes/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,18 @@ export class Queue<
name: NameType,
repeatOpts: RepeatOptions,
jobId?: string,
) {
return (await this.repeat).removeRepeatable(name, repeatOpts, jobId);
): Promise<boolean> {
const repeat = await this.repeat;
const removed = await repeat.removeRepeatable(name, repeatOpts, jobId);

return !removed;
}

async removeRepeatableByKey(key: string) {
return (await this.repeat).removeRepeatableByKey(key);
async removeRepeatableByKey(key: string): Promise<boolean> {
const repeat = await this.repeat;
const removed = await repeat.removeRepeatableByKey(key);

return !removed;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/classes/repeat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export class Repeat extends QueueBase {
name: string,
repeat: RepeatOptions,
jobId?: string,
): Promise<void> {
): Promise<number> {
const repeatJobKey = getRepeatKey(name, { ...repeat, jobId });
const repeatJobId = getRepeatJobId(
name,
Expand All @@ -130,7 +130,7 @@ export class Repeat extends QueueBase {
return Scripts.removeRepeatable(this, repeatJobId, repeatJobKey);
}

async removeRepeatableByKey(repeatJobKey: string): Promise<void> {
async removeRepeatableByKey(repeatJobKey: string): Promise<number> {
const data = this.keyToData(repeatJobKey);

const repeatJobId = getRepeatJobId(
Expand Down
2 changes: 1 addition & 1 deletion src/classes/scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export class Scripts {
queue: MinimalQueue,
repeatJobId: string,
repeatJobKey: string,
): Promise<void> {
): Promise<number> {
const client = await queue.client;
const args = this.removeRepeatableArgs(queue, repeatJobId, repeatJobKey);

Expand Down
4 changes: 2 additions & 2 deletions src/commands/moveStalledJobsToWait-8.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ if (#stalling > 0) then
if opts["removeOnFail"] then
removeJob(jobId, false, ARGV[2])
rcall("ZREM", KEYS[4], jobId)
end
end
elseif removeOnFailType ~= "nil" then
local maxAge = opts["removeOnFail"]["age"]
local maxCount = opts["removeOnFail"]["count"]

if maxAge ~= nil then
removeJobsByMaxAge(ARGV[3], maxAge, KEYS[4], ARGV[2])
end

if maxCount ~= nil and maxCount > 0 then
removeJobsByMaxCount(maxCount, KEYS[4], ARGV[2])
end
Expand Down
10 changes: 9 additions & 1 deletion src/commands/removeRepeatable-2.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
ARGV[1] repeat job id
ARGV[2] repeat job key
ARGV[3] queue key
Output:
0 - OK
1 - Missing repeat job
]]
local rcall = redis.call
local millis = rcall("ZSCORE", KEYS[1], ARGV[2])
Expand All @@ -21,4 +25,8 @@ if(millis) then
end
end

rcall("ZREM", KEYS[1], ARGV[2]);
if(rcall("ZREM", KEYS[1], ARGV[2]) == 1) then
return 0
end

return 1
22 changes: 19 additions & 3 deletions tests/test_repeat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -752,10 +752,11 @@ describe('repeat', function () {
let processor;

const processing = new Promise<void>((resolve, reject) => {
processor = async (job: Job) => {
processor = async () => {
counter++;
if (counter == numJobs) {
await queue.removeRepeatable('remove', repeat);
const removed = await queue.removeRepeatable('remove', repeat);
expect(removed).to.be.true;
this.clock.tick(nextTick);
const delayed = await queue.getDelayed();
expect(delayed).to.be.empty;
Expand Down Expand Up @@ -793,11 +794,26 @@ describe('repeat', function () {
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 removed = await queue.removeRepeatableByKey(repeatableJobs[0].key);
expect(removed).to.be.true;
const repeatableJobsAfterRemove = await queue.getRepeatableJobs();
expect(repeatableJobsAfterRemove).to.have.length(0);
});

describe('when repeatable job does not exist', function () {
it('returns false', async () => {
const repeat = { cron: '*/2 * * * * *' };

await queue.add('remove', { foo: 'bar' }, { repeat });
const repeatableJobs = await queue.getRepeatableJobs();
expect(repeatableJobs).to.have.length(1);
const removed = await queue.removeRepeatableByKey(repeatableJobs[0].key);
expect(removed).to.be.true;
const removed2 = await queue.removeRepeatableByKey(repeatableJobs[0].key);
expect(removed2).to.be.false;
});
});

it('should allow removing a customId repeatable job', async function () {
const queueScheduler = new QueueScheduler(queueName, { connection });
await queueScheduler.waitUntilReady();
Expand Down

0 comments on commit 59b0da7

Please sign in to comment.