Let user start celery task, and let user know when task is completed

187 Views Asked by At

I have a profile page where user can see all his information.

I want to create a button so the user can pull some specific external information.

The button should start a celery task, and when the task is completed, the user should get a message with "completed"

How can I do this?

<a href="/start-task/">Pull external info</a>

My "start-task" view:

def start_task(request):

    get_external_user_info.delay(user=request.user)

    return HttpResponse("Task started")

But how do I do a ajax check if the task is completed?

Do I need another view? How would that view look like?

1

There are 1 best solutions below

0
On BEST ANSWER

You have to get task_id

x= get_external_user_info.delay(user=request.user)
id = x.task_id
return HttpResponse(id)

After that using this id to ajax and get status

AsyncResult(id).state

Check this Celery task status