Worker sometimes loose connection and needs to be restarted with psycopg2
Jeanbouvatt opened this issue · comments
Jeanbouvatt commented
Similar to #216 , My worker sometimes loose the connection and must be restarted
[...]
cursor = self.connection.cursor()
psycopg2.InterfaceError: connection already closed
My env :
python==3.8.13
Django==3.2.16
django-rq==2.6.0
psycopg2==2.9.5
redis==4.4.0rc4
rq==1.11.1
Jeanbouvatt commented
calling django.db.close_old_connections
at the beginning of all our task solved our problem in production, however our test suit ceased to work in CI/CD, probably because our test suit is run with ASYNC_QUEUES=False.
My solution was to patch django.db.close_old_connections
in test suit so it does not trigger.
Annoying though.
Jeanbouvatt commented
it seems the connection is lost when a Django database exception occurs.
A simple example:
def task():
User.objects.get(id=non_existent_id) # Obviously fails
except Exception:
User.objects.all().delete() # This will raise another exception : connection is closed