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

Add retry logic for Django and Psycopg2 InterfaceErrors #290

Merged
merged 2 commits into from Mar 1, 2022
Merged

Add retry logic for Django and Psycopg2 InterfaceErrors #290

merged 2 commits into from Mar 1, 2022

Conversation

juspence
Copy link
Contributor

This PR implements #119 in a slightly different way. It adds retry logic only for django.db.utils.InterfaceError and psycopg2.InterfaceError, which can be errors like "connection already closed".

It does not retry any DatabaseError, which can be errors like "numeric value out of range" or "foreign key check failed". See https://www.psycopg.org/docs/module.html#exceptions for more info.

exception_safe_to_retry() is overridden from the parent class: https://docs.celeryproject.org/en/stable/_modules/celery/backends/base.html#BaseBackend

This method is called when an exception is raised in _store_result() and always_retry / result_backend_always_retry is set to True. If the exception is an InterfaceError, the connection is closed as a side effect of calling exception_safe_to_retry().

Then the retry logic implemented in the parent class continues, and Django will automatically open a new connection for the next attempt to store the task result or get the task metadata.

Copy link
Member

@auvipy auvipy left a comment

Choose a reason for hiding this comment

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

I would like to see unit tests for this change

@auvipy
Copy link
Member

auvipy commented Feb 28, 2022

I want to add this to next release, can you please address the review comment?

@juspence
Copy link
Contributor Author

@auvipy Yes, I am working on this today since I had other tasks to finish Friday.

@auvipy
Copy link
Member

auvipy commented Feb 28, 2022

ok

@juspence juspence requested a review from auvipy February 28, 2022 21:12
@auvipy auvipy merged commit 28fb1c6 into celery:master Mar 1, 2022
from kombu.exceptions import DecodeError
from psycopg2 import InterfaceError as Psycopg2InterfaceError
Copy link
Contributor

Choose a reason for hiding this comment

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

This adds a hard dependency on psycopg2, but django-celery-results can be used with other databases as well.

I have opened PR #292 to fix this.

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

Successfully merging this pull request may close these issues.

None yet

3 participants