Search code examples
pythondjangoredispython-huey

Django and Huey task issue, record in DB doesn't exist when the task is ran


I am testing Huey with Django and found one issue, tasks can't find the record in DB.

  • Postgres 14
  • Redis 7.0.5
  • Django 4
  • use with docker

Here is the code:

# settings.py
USE_HUEY = env.bool("USE_HUEY", False)
HUEY = {
    "huey_class": "huey.RedisHuey", 
    "name": "huey",
    "immediate": not USE_HUEY,
    "connection": {"url": REDIS_URL},
}


# app/signals.py
@receiver(post_save, sender=Post)
def post_signal(sender, instance, **kwargs):
    from app.tasks import create_or_update_related_objects
    create_or_update_related_objects(instance.pk)


# app/tasks.py
@db_task()
def create_or_update_related_objects(object_pk):
    post = Post.objects.get(pk=object_pk)
    ...

This is running an async task but I am getting the error:

app.models.Post.DoesNotExist: Post matching query does not exist.

This is not correct, there is a post, and this task is running on a post_save signal.

What is weird, is if I do something like this, it works fine:

@db_task()
def create_or_update_related_objects(object_pk):
    import time
    time.sleep(3)
    post = Post.objects.get(pk=object_pk)
    ...

What am I doing wrong here?


Solution

  • Solution is to use transaction.on_commit on signal.py:

    # app/signals.py
    from django.db import transaction
    
    
    @receiver(post_save, sender=Post)
    def post_signal(sender, instance, **kwargs):
        from app.tasks import create_or_update_related_objects
        transaction.on_commit(lambda: create_or_update_related_objects(instance.pk))
    

    This way I am sure that Post object is saved to DB.