I'm building a REST API in Django where users can run tasks for objects. I've created a simple example below where I use transactions to easily rollback the state of the object in case the task fails.
from django.db import IntegrityError, transaction
def send_object(id):
obj = MyModel.objects.get(pk=id)
if obj.state != 'Created':
raise ValueError('Object not in state "Created"')
with transaction.atomic():
obj.state = 'Sending'
obj.save()
# send the object...
obj.state = 'Sent'
obj.save()
Now I want the state of the object to be visible for users through the API while the task is running. Since the transaction isn't commited until the task is done, users only see the current state before and after the task.
I need some sort of transaction in case of a system failure/shutdown occurs during the task and the object gets stuck at 'Sending'. The state must be reset so that the task easily can be retried.
The isolation level is set to READ COMMITTED
and cannot be changed without updating other parts of the code.
Can I make the new state available before the transaction is commited (i.e. enable dirty reads) for just this transaction? Or is there something else I can do?