From d674db14da51093b5e1acc7daf33e7d900b76053 Mon Sep 17 00:00:00 2001 From: clearml <> Date: Thu, 2 Jan 2025 00:14:52 +0200 Subject: [PATCH] Add missing stop_request, request a an agent to stop a running Task gracefully --- clearml/backend_interface/task/task.py | 22 ++++++++++++++++++++++ clearml/task.py | 20 ++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/clearml/backend_interface/task/task.py b/clearml/backend_interface/task/task.py index 331c7343..b83a4d50 100644 --- a/clearml/backend_interface/task/task.py +++ b/clearml/backend_interface/task/task.py @@ -695,6 +695,28 @@ class Task(IdObjectBase, AccessMixin, SetupUploadMixin): ignore_errors=ignore_errors ) + def stop_request(self, ignore_errors=True, force=False, status_message=None): + # type: (bool, bool, Optional[str]) -> () + """ + Request a task to stop. this will not change the task status + but mark a request for an agent or SDK to actually stop the Task. + This will trigger the Task's abort callback, and at the end will + change the task status to stopped and kill the Task's processes + + Notice: calling this on your own Task, will cause + the watchdog to call the on_abort callback and kill the process + + :param bool force: If not True, call fails if the task status is not 'in_progress' + :param bool ignore_errors: if False raise exception on error + :param str status_message: Optional, add status change message to the stop request. + This message will be stored as status_message on the Task's info panel + """ + # request task stop + return self.send( + tasks.StopRequest(self.id, force=force, status_reason="abort request", status_message=status_message), + ignore_errors=ignore_errors + ) + def completed(self, ignore_errors=True): # type: (bool) -> () """ diff --git a/clearml/task.py b/clearml/task.py index d4227b60..9c5cc881 100644 --- a/clearml/task.py +++ b/clearml/task.py @@ -2406,6 +2406,26 @@ class Task(_Task): # mark task as stopped self.stopped(force=force, status_message=str(status_message) if status_message else None) + def mark_stop_request(self, force=False, status_message=None): + # type: (bool, Optional[str]) -> () + """ + Request a task to stop. this will not change the task status + but mark a request for an agent or SDK to actually stop the Task. + This will trigger the Task's abort callback, and at the end will + change the task status to stopped and kill the Task's processes + + Notice: calling this on your own Task, will cause + the watchdog to call the on_abort callback and kill the process + + :param bool force: If not True, call fails if the task status is not 'in_progress' + :param str status_message: Optional, add status change message to the stop request. + This message will be stored as status_message on the Task's info panel + """ + # flush any outstanding logs + self.flush(wait_for_uploads=True) + # request task stop + return self.stop_request(self, force=force, status_message=status_message) + def flush(self, wait_for_uploads=False): # type: (bool) -> bool """