Skip to content

Throw an exception from Celery retry() #8905

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

Merged
merged 1 commit into from
Feb 14, 2022
Merged

Conversation

agjohnson
Copy link
Contributor

@agjohnson agjohnson commented Feb 8, 2022

Without an exception here, or handling the return from
_check_concurrency_limit, a build that hits a concurrency limit in
this method will both set a retry on the task, but will also continue
processing the current task like normal. This results in two identical
builds, then three, then four, and so on.

Untested what this does to the UI though. Edit: the UI reports the retry as I'd expect 👍

Without an exception here, or handling the return from
`_check_concurrency_limit`, a build that hits a concurrency limit in
this method will both set a retry on the task, but will also continue
processing the current task like normal. This results in two identical
builds, then three, then four, and so on.

Untested what this does to the UI though.
@agjohnson agjohnson requested a review from a team as a code owner February 8, 2022 23:16
@agjohnson
Copy link
Contributor Author

cc @humitos as you might have more background on why throws=False might be required here.

@agjohnson agjohnson added the PR: hotfix Pull request applied as hotfix to release label Feb 8, 2022
Copy link
Member

@ericholscher ericholscher left a comment

Choose a reason for hiding this comment

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

I'm guessing this is just copied from before, where we were not handling exceptions nicely, and were explicitly returning False after this line.

@humitos
Copy link
Member

humitos commented Feb 9, 2022

This looks good for now as a quick fix and it seems that solved the problem, so 🎉

However, I just wanted to note here that I need to check a little deeper the workflow to understand what's going on:

  • when calling self.retry(throw=True), does it go to on_retry or to on_failure?
    • If it goes to on_failure we will be sending a notification when we shouldn't
    • inside on_failure we are not populating the {limit} variable from the error user message
  • what happens with the autoretry_for attribute from the class at
    autoretry_for = (
    BuildMaxConcurrencyError,
    )
    ? is it working?

I'm happy merging this, but I want to come back here and do some tests to understand exactly what's happening so we can document it properly and execute the exact code we want for each case.

@humitos humitos self-assigned this Feb 9, 2022
@humitos
Copy link
Member

humitos commented Feb 9, 2022

Also, I saw that there is still a problem with the concurrency limit still: https://sentry.io/organizations/read-the-docs/issues/2998694596/ that I want to come back and double check.

@agjohnson
Copy link
Contributor Author

Ahh yeah good catch, looks like this PR isn't the only fix we need here. Shall we merge this so it's not sitting around hotfix, and move the remaining pieces to a separate issue?

@humitos
Copy link
Member

humitos commented Feb 14, 2022

Yes. I'm merging this PR. The test issue seems unrelated.

@humitos humitos merged commit 3043d62 into master Feb 14, 2022
@humitos humitos deleted the hotfix/celery-retry-exception branch February 14, 2022 20:04
@humitos
Copy link
Member

humitos commented Feb 14, 2022

when calling self.retry(throw=True), does it go to on_retry or to on_failure?

It goes to on_retry. We can check for isinstance(exc, BuildMaxConcurrencyError) and execute the code we need there (e.g. update the build object, etc). This way we will be handling all the retries in the same place.

what happens with the autoretry_for attribute from the class at

This works perfectly. However, to make usage of this attribute we need to raise BuildMaxConcurrencyError from inside the task instead of manually call self.retry. See https://docs.celeryproject.org/en/master/userguide/tasks.html#automatic-retry-for-known-exceptions

I'm opening a PR with these changes to make usage of these Celery features.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: hotfix Pull request applied as hotfix to release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants