How to pause and stop celery task from django template

Viewed 749

I am working on a django application that uses celery to run tasks asynchronously. Right now a user can submit a form from the webpage to start a celery task. But there is no way to pause or stop the task on the click of a button inside a django template.

this is my code so far

celery task

@shared_task
def get_website(website):
    website_list = return_website_list(website)

    return website_list

In the above task I am calling a return_website_list() function that scrapes the required website and returns a list of links from the website.

output.html template

<div class="container">
    <button class="pause_btn" type="button">Pause task</button>
    <button class="resume_btn" type="button">Resume task</button>
    <button class="stop_btn" type="button">Stop task</button>
</div>

I want the ability to pause the task indefinitely when the pause button is clicked and resume the task when the resume button is clicked or the ability to stop the task completely when the stop button is clicked.

views.py

def index(request):

    if request.method == 'POST':
        website = request.POST.get('website-name')
   
        get_website.delay(website)
        return redirect('output')

    return render(request, 'index.html')

I searched online, like these link1, link2, link3. But these links did not help me achieve what I am trying to do.

Thanks in advance

1 Answers

my idea is a field named state and state will set from pause button click. and in celery task in each action check state and if it is in paused state wait for 1 second and check again. i used this idea previously for cancel action

Related