Django: celery showing different status for the same task

Viewed 58

I've a django app with two celery tasks, say task1 and task2. task1 gets triggered on a POST request from the frontend and the request has to sent back the task id so the frontend can poll its status later. task2 is supposed to run after task1 succeeds, asynchronously.

Here're my task definitions:

@shared_task(ignore_result=False)
def task1(pk: int):
    # retrieve object matching pk
    return task1_func(obj)


@shared_task(ignore_result=False)
def task2(task_id: int):
    task = AsyncResult(task_id)
    while not task.ready():
       logger.info("status {}".format(task.status))
        time.sleep(1)
    return task2_func()

Here's how I'm triggering them inside my views.py:

class MyView(viewsets.ModelViewSet):
    def create(self, request):
        # create object

        task = task1.delay(obj.pk)
        task2.delay(task.id)
        #breakpoint
        return Response(task.id, status=status.HTTP_200_OK)

Now when I try checking the status at the breakpoint, it shows success:

In [1]: task.id                                                                         
Out[1]: '97f8d150-de04-4848-a400-3882988c833c'                                          

In [2]: task.status                                                                     
Out[2]: 'SUCCESS'                                                                       

In [3]: AsyncResult(task.id).status                                                     
Out[3]: 'SUCCESS' 

But the code inside the task2 never gets executed because the status of task1 inside task2, never changes from PENDING. What am I doing wrong? Clearly my results are being stored and accessible but for some reason I cannot see the change in status from within another task. I cannot chain the tasks because I've to send the first task's id to the frontend.

0 Answers
Related