skip to Main Content

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?

2

Answers


  1. Chosen as BEST ANSWER

    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.


  2. I’m not sure, but most likely your task will be completed before the database is committed. It seems that the enqueue() method can add your task to the queue so that the database has time to commit.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search