-
Notifications
You must be signed in to change notification settings - Fork 357
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(queue): add retryJobs method #1024
Conversation
src/classes/queue.ts
Outdated
@@ -361,6 +361,13 @@ export class Queue< | |||
} while (cursor); | |||
} | |||
|
|||
async retryAllFailedJobs(count?: number): Promise<void> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probable we could rename this as retryFailedJobs, to be shorter 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the minimal name would be just "retryFailed", or even just "retry" if in the future we would allow retry also "completed" jobs, which is not completely unlikely, the case being, a lot of jobs completed but they did not do the proper job, so we want to retry all of them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe "retryJobs" then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds good to me, I will change the count prop to be into an object, so when we introduce the case for completed jobs, a new param could be place into this object
|
||
for i, key in ipairs(jobs) do | ||
local jobKey = baseKey .. key | ||
if (rcall("ZREM", KEYS[3], key) == 1) then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should not be needed to check that we in fact removed the value from the zset since .lua scripts are atomic and we got the value from a previous call to ZRANGE. By skipping this test we can remove the values much faster in one call (up to 7000 values since there is an issue with more than 8k values in Redis), check how we did it here: https://github.com/taskforcesh/bullmq/blob/master/src/commands/moveStalledJobsToWait-8.lua#L103-L107
Same concept can be applied to LPUSH, however not possible with HDEL and HSET. I think the reset of finishOn, etc could be made when moving the job to active so that it is done lazily and therefore this call will become much faster, what do you think?
Also, regarding attemptsMade, I see there is a point in resetting it, but not sure we must, that would also make this call faster as we do not need to iterate through all the keys one by one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sadly we cannot optimize the call to XADD, so that will still need to be iterated...
src/classes/queue.ts
Outdated
@@ -361,6 +361,13 @@ export class Queue< | |||
} while (cursor); | |||
} | |||
|
|||
async retryAllFailedJobs(count?: number): Promise<void> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the minimal name would be just "retryFailed", or even just "retry" if in the future we would allow retry also "completed" jobs, which is not completely unlikely, the case being, a lot of jobs completed but they did not do the proper job, so we want to retry all of them.
src/classes/queue.ts
Outdated
@@ -361,6 +361,13 @@ export class Queue< | |||
} while (cursor); | |||
} | |||
|
|||
async retryAllFailedJobs(count?: number): Promise<void> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe "retryJobs" then.
tests/test_queue.ts
Outdated
}); | ||
|
||
fail = false; | ||
await queue.retryAllFailedJobs(2); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is also important to check that the order of the retried jobs is correct, as the oldest failed must be the first to be retried.
Note: we need to backport this feature to Bull aswell. |
src/commands/retryJobs-4.lua
Outdated
return 1 | ||
end | ||
|
||
return 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Notice that there is no newline at the end.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great, just a small comment regarding the documentation of the retryAll method.
@@ -361,6 +361,13 @@ export class Queue< | |||
} while (cursor); | |||
} | |||
|
|||
async retryJobs(opts: { count?: number } = {}): Promise<void> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should add a documentation snippet for this method so that it appears in the API documentation.
@@ -0,0 +1,12 @@ | |||
local function batches(n, batchSize) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice refactoring
src/commands/retryJobs-4.lua
Outdated
rcall("XADD", KEYS[2], "*", "event", "waiting", "jobId", key); | ||
end | ||
|
||
if (#jobs > 0) then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This "if" can also be moved to include also the "ipairs" loop
end | ||
|
||
if (#jobs > 0) then | ||
for from, to in batches(#jobs, 7000) do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice
🎉 This PR is included in version 1.68.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
fixes #1001