Reputation: 61
I've been successful in creating a repeating task in Django Background-Tasks that I can start with a button on my web page.
I know need a way to stop this repeating tasks again with another button.
Reading the documentation and here on stackoverflow I cannot seem to see a way in background-tasks which can eliminate this task again (for my stop button).
What would be a nice clean solution to solve this?
Upvotes: 1
Views: 1538
Reputation: 61
After some help on the Reddit /djangolearning I came to the following solution:
There is a table (model) in the database: background_tasks. This table contains the task queue.
I imported the task model as follows:
from background_task.models import Task
Next thing I did was look for the task I was trying to kill, and kill it:
def kill_background_task(appname, task_name):
background_tasks = Task.objects.filter(task_name='appname.background_tasks.task_name')
for background_task in background_tasks:
background_task.delete()
I opted for this iterable loop to make sure if some task was somehow started twice or even more times they would all get deleted.
Upvotes: 4