Skip to content
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

Merged
merged 11 commits into from
Jan 29, 2022
Merged

feat(queue): add retryJobs method #1024

merged 11 commits into from
Jan 29, 2022

Conversation

roggervalf
Copy link
Collaborator

@roggervalf roggervalf commented Jan 26, 2022

fixes #1001

@@ -361,6 +361,13 @@ export class Queue<
} while (cursor);
}

async retryAllFailedJobs(count?: number): Promise<void> {
Copy link
Collaborator Author

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 🤔

Copy link
Contributor

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe "retryJobs" then.

Copy link
Collaborator Author

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
Copy link
Contributor

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.

Copy link
Contributor

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...

@@ -361,6 +361,13 @@ export class Queue<
} while (cursor);
}

async retryAllFailedJobs(count?: number): Promise<void> {
Copy link
Contributor

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.

@@ -361,6 +361,13 @@ export class Queue<
} while (cursor);
}

async retryAllFailedJobs(count?: number): Promise<void> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe "retryJobs" then.

});

fail = false;
await queue.retryAllFailedJobs(2);
Copy link
Contributor

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.

@manast
Copy link
Contributor

manast commented Jan 26, 2022

Note: we need to backport this feature to Bull aswell.

return 1
end

return 0
Copy link
Contributor

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.

@roggervalf roggervalf changed the title feat(queue): add retryAllFailedJobs method feat(queue): add retryJobs method Jan 27, 2022
Copy link
Contributor

@manast manast left a 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> {
Copy link
Contributor

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice refactoring

rcall("XADD", KEYS[2], "*", "event", "waiting", "jobId", key);
end

if (#jobs > 0) then
Copy link
Contributor

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice

@roggervalf roggervalf merged commit 310a730 into master Jan 29, 2022
@roggervalf roggervalf deleted the retry-all-failed-jobs branch January 29, 2022 14:52
github-actions bot pushed a commit that referenced this pull request Jan 29, 2022
# [1.68.0](v1.67.3...v1.68.0) (2022-01-29)

### Features

* **queue:** add retryJobs method for failed jobs ([#1024](#1024)) ([310a730](310a730))

### Performance Improvements

* **lua:** call del command with multiple keys ([#1035](#1035)) ([9cfaab8](9cfaab8))
@github-actions
Copy link

🎉 This PR is included in version 1.68.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for an efficient "retry all" failed jobs
2 participants