-
Notifications
You must be signed in to change notification settings - Fork 454
Support celerycam for reconnecting db connenction if db connection is lost accidentally #550
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
base: master
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,6 +4,8 @@ | |||||||||||||||||||||||||||||||||||||||||||||
| from datetime import timedelta | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| from django.conf import settings | ||||||||||||||||||||||||||||||||||||||||||||||
| from django.db import connection | ||||||||||||||||||||||||||||||||||||||||||||||
| from django.db.utils import InterfaceError | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| from celery import states | ||||||||||||||||||||||||||||||||||||||||||||||
| from celery.events.state import Task | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -127,9 +129,18 @@ def _handle_tasks(): | |||||||||||||||||||||||||||||||||||||||||||||
| for i, task in enumerate(state.tasks.items()): | ||||||||||||||||||||||||||||||||||||||||||||||
| self.handle_task(task) | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| for worker in state.workers.items(): | ||||||||||||||||||||||||||||||||||||||||||||||
| self.handle_worker(worker) | ||||||||||||||||||||||||||||||||||||||||||||||
| _handle_tasks() | ||||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||||
| for worker in state.workers.items(): | ||||||||||||||||||||||||||||||||||||||||||||||
| self.handle_worker(worker) | ||||||||||||||||||||||||||||||||||||||||||||||
| _handle_tasks() | ||||||||||||||||||||||||||||||||||||||||||||||
| except InterfaceError as e: | ||||||||||||||||||||||||||||||||||||||||||||||
| # When connection already closed exception is raised, | ||||||||||||||||||||||||||||||||||||||||||||||
| # force to close connection and Django will automatically reconnect | ||||||||||||||||||||||||||||||||||||||||||||||
| if str(e) == 'connection already closed': | ||||||||||||||||||||||||||||||||||||||||||||||
| connection.close() | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
| try: | |
| for worker in state.workers.items(): | |
| self.handle_worker(worker) | |
| _handle_tasks() | |
| except InterfaceError as e: | |
| # When connection already closed exception is raised, | |
| # force to close connection and Django will automatically reconnect | |
| if str(e) == 'connection already closed': | |
| connection.close() | |
| for worker in state.workers.items(): | |
| self.handle_worker(worker) | |
| _handle_tasks() | |
| try: | |
| # Check and handle database reconnection if needed | |
| connection.close() | |
| except InterfaceError as e: | |
| # When connection already closed exception is raised, | |
| # force to close connection and Django will automatically reconnect | |
| if str(e) == 'connection already closed': |
Copilot
AI
May 20, 2025
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.
Instead of calling connection.close() directly, use django.db.close_old_connections() to properly handle connection pooling and ensure thread safety according to Django's recommendations.
| connection.close() | |
| close_old_connections() |
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.
Comparing exception message strings is brittle; consider catching a more specific exception subclass or inspecting error attributes instead of matching on
str(e).