Celery Task Fails with "Object Does Not Exist" Error Despite Using Auto-Commit in Django
In my Django application, I've encountered a rare issue where a Celery task, invoked via apply_async immediately after creating an object in a model's save method, failed because it could not find the newly created object. This issue has occurred only once, making it particularly puzzling given Django's auto-commit mode, which should commit the new object to the database immediately. Below is a simplified version of the code where the issue appeared:
from django.db import models from myapp.tasks import process_new_object
class MyModel(models.Model):
....
class AnotherModel(models.Model):
....
def save(self, *args, **kwargs):
new_obj = MyModel.objects.create(name="Newly Created Object")
process_new_object.apply_async(args=[new_obj.pk], countdown=5)
return super().save(*args, **kwargs)
@shared_task
def process_new_object(obj_id):
try:
obj = MyModel.objects.get(pk=obj_id)
...
except MyModel.DoesNotExist:
pass
what might be potential causes for the Celery task to sometimes fail to find the newly created object, despite the delay and Django's auto-commit?