django-rq
django-rq copied to clipboard
Provide an option to wrap job execution in Django transaction
A proposal:
from django.db import close_old_connections, reset_queries, transaction
import rq
class TransactionWorker(rq.Worker):
def perform_job(self, job):
reset_queries()
close_old_connections()
try:
with transaction.atomic():
super(TransactionWorker, self).perform_job(job)
finally:
close_old_connections()
I have extended a bit the scope of this ticket It is important to re-assure there is a valid DB connection At the moment if the rqworker loses the db connection you should restart it But django request/response cycle has already the ability to reconnect: https://github.com/django/django/blob/master/django/db/init.py
So I propose to use the same functions inside a new django_rq.TransactionWorker
... Maybe the "transaction.atomic" could be activated only if settings.DATABASES["default"]["ATOMIC_REQUESTS"] is set ...
I changed my mind. I think it is better I don't overlap responsibilities between transaction-management and connection-management The user should simply simply use the decorator transaction.atomic to wrap job execution in a Django transaction In this way it is up to the user to correctly decorate the function enqueued and django-rq can be agnostic about it I propose to close this ticket On the other side, the connection management should be up to the framework: the same policy django uses to manage databases and connections should be used by django-rq workers I have created another ticket specific to this last issue: #216