2

I have a Django application and React as my frontend framework and I am using celery for running lengthy tasks which do take a lot of time and everything is working fine.

Also I am providing real time status of the each task running on the celery via celery signals and all . Also I have created a database table which stores the status of all the tasks that are running , like when the task is created , i create a row with the parameters and they are unique so that task with same parameters won't run if its already running or is in the queue , so the user interface is good . But there are few tasks which need to stop and also the user have the authority and permission to stop the tasks that are running as i am updating every user for the tasks. Now if the task is in the queue i can use revoke to stop the from being executed .

from celery.task.control import revoke
revoke(task_id, terminate=True)

But the thing is that , i have tasks which are very lengthy , so the user may want to stop the task and focus on other tasks .

I want to stop the current task which is currently being run by the worker . Suppose a task T1 is being executed by the worker , i want to stop that , I have seen every part of internet but could not find a way to do so .

User may want to stop the task and I want to provide that functionality to user for stopping the current running task.

I have also viewed different task management modules like Dramatiq but still you cannot stop the current working task . I have all the data to stop the task like task id and all , but could not find a way to do so .

Any help would be great . Thanks .

Om Kashyap
  • 118
  • 8

1 Answers1

1

One of the dirty ways of doing this is by sending a SIGKILL to the child worker process that is actually executing the task. Main worker process will respawns the child if it gets killed and will execute other tasks.

For sending a signal, you have to manually issue a SIGKILL to the PID from the server where celery is running. You have to have some other process(probably API) listening for your commands and send SIGKILL.

Lemon Reddy
  • 553
  • 3
  • 5
  • Can you show me that with the code , that would be a great help . – Om Kashyap Jul 24 '23 at 08:09
  • Celery also has a something called AbortableTask. Maybe if it fits your use case, use that. https://docs.celeryq.dev/en/stable/reference/celery.contrib.abortable.html – Lemon Reddy Jul 25 '23 at 15:20