5

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

Sashaank
  • 880
  • 2
  • 20
  • 54
  • 1
    afaik, there is no built-in API in Celery. Also, you may need some kind of *custom logic* to obtain the ***PAUSE*** feature, since Django/Celery/Redis(or any other broker) won't able to know where was the last execution point ***for each task*** – JPG Sep 08 '20 at 10:58
  • BTW, you can terminate (aka Stop) the task, more easily than Pause. Ref: [Cancel an already executing task with Celery?](https://stackoverflow.com/questions/8920643/cancel-an-already-executing-task-with-celery) – JPG Sep 08 '20 at 11:00
  • I recently gave this answer for a similar question - "How to manage a stop or restart of a task from Django-based site? ": https://stackoverflow.com/a/62809499/2785080 you could easily adapt it for your usage needs. Let me know if you need further explaination. – Ben Sep 08 '20 at 17:03
  • @Ben I saw your answer. How do I resume a task that partially executed with your logic? – JPG Sep 09 '20 at 05:38
  • @Ben Hi! thanks for the reply. In your answer you create a model `ScriptTracker()` right? Can you explain where I would put the `while` loop in my view? Sorry I am still a novice in django and celery and would really like you help – Sashaank Sep 09 '20 at 09:27
  • Does this question (and answer) provide any useful information? It's possible to do I believe, but it'd require linking up a view to fiddle with celery similar to what's seen here; https://stackoverflow.com/questions/48431599/pause-celery-task – markwalker_ Sep 09 '20 at 11:57

1 Answers1

-1

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

sahama
  • 669
  • 8
  • 16