File tree Expand file tree Collapse file tree 1 file changed +12
-3
lines changed
Expand file tree Collapse file tree 1 file changed +12
-3
lines changed Original file line number Diff line number Diff line change 44from datetime import timedelta
55
66from django .conf import settings
7+ from django .db import connection
8+ from django .db .utils import InterfaceError
79
810from celery import states
911from celery .events .state import Task
@@ -127,9 +129,16 @@ def _handle_tasks():
127129 for i , task in enumerate (state .tasks .items ()):
128130 self .handle_task (task )
129131
130- for worker in state .workers .items ():
131- self .handle_worker (worker )
132- _handle_tasks ()
132+ try :
133+ for worker in state .workers .items ():
134+ self .handle_worker (worker )
135+ _handle_tasks ()
136+ except InterfaceError as e :
137+ # When connection already closed exception is raised,
138+ # force to close connection and Django will automatically reconnect
139+ if str (e ) == 'connection already closed' :
140+ connction .close ()
141+ logger .info ('Django db connection is closed and will reconnect' )
133142
134143 def on_cleanup (self ):
135144 expired = (self .TaskState .objects .expire_by_states (states , expires )
You can’t perform that action at this time.
0 commit comments