Skip to content

Commit

Permalink
OpenConceptLab/ocl_issues#1777 | added task post run signal to close …
Browse files Browse the repository at this point in the history
…db connections
  • Loading branch information
snyaggarwal committed Apr 24, 2024
1 parent 794c2c8 commit caf56fb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
'core.importers',
'core.pins',
'core.client_configs',
'core.tasks',
'core.tasks.apps.TaskConfig',
'core.toggles',
'core.repos',
'core.url_registry',
Expand Down
9 changes: 9 additions & 0 deletions core/tasks/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.apps import AppConfig


class TaskConfig(AppConfig):
name = 'core.tasks'
verbose_name = "Task"

def ready(self):
from core.tasks import signals # pylint: disable=unused-variable, unused-import
15 changes: 15 additions & 0 deletions core/tasks/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from celery.signals import task_postrun
from django import db


@task_postrun.connect
def on_task_done(sender=None, headers=None, body=None, **kwargs):
for conn in db.connections.all():
try:
conn.close_if_unusable_or_obsolete()
except db.utils.InterfaceError:
pass
except db.DatabaseError as exc:
str_exc = str(exc)
if 'closed' not in str_exc and 'not connected' not in str_exc:
raise exc

0 comments on commit caf56fb

Please sign in to comment.