mirror of
https://github.com/tiennm99/serena.git
synced 2026-10-12 22:14:54 +00:00
Declutter task cancelling
This commit is contained in:
1 parent
322348ea51
commit
7d836173fc
3 files changed
+9
-30
No files matched your search
@@ -198,15 +198,6 @@ class SerenaAgent:
|
||||
"""
|
||||
return self._task_executor.get_current_tasks()
|
||||
|
||||
def cancel_task(self, task_id: int) -> bool:
|
||||
"""
|
||||
Cancels a task with the given ID and removes it from the task queue.
|
||||
|
||||
:param task_id: the ID of the task to cancel
|
||||
:return: True if the task was found and cancelled, False otherwise
|
||||
"""
|
||||
return self._task_executor.cancel_task(task_id)
|
||||
|
||||
def get_last_executed_task(self) -> TaskExecutor.TaskInfo | None:
|
||||
"""
|
||||
Gets the last executed task.
|
||||
|
||||
@@ -262,8 +262,15 @@ class SerenaDashboardAPI:
|
||||
request_data = request.get_json()
|
||||
try:
|
||||
request_cancel_task = RequestCancelTaskExecution.model_validate(request_data)
|
||||
task_was_cancelled = self._agent.cancel_task(request_cancel_task.task_id)
|
||||
return {"status": "success", "was_cancelled": task_was_cancelled}
|
||||
for task in self._agent.get_current_tasks():
|
||||
if task.task_id == request_cancel_task.task_id:
|
||||
task.cancel()
|
||||
return {"status": "success", "was_cancelled": True}
|
||||
return {
|
||||
"status": "success",
|
||||
"was_cancelled": False,
|
||||
"message": f"Task with id {request_data.get('task_id')} not found, maybe execution was already finished",
|
||||
}
|
||||
except Exception as e:
|
||||
return {"status": "error", "message": str(e), "was_cancelled": False}
|
||||
|
||||
|
||||
@@ -216,22 +216,3 @@ class TaskExecutor:
|
||||
"""
|
||||
with self._task_executor_lock:
|
||||
return self._task_executor_last_executed_task_info
|
||||
|
||||
def cancel_task(self, task_id: int) -> bool:
|
||||
"""
|
||||
Cancels a task with the given task ID and removes it from the queue if found.
|
||||
|
||||
:param task_id: the unique identifier of the task to cancel
|
||||
:return: True if the task was found and cancelled, False otherwise
|
||||
"""
|
||||
with self._task_executor_lock:
|
||||
if self._task_executor_current_task is not None and id(self._task_executor_current_task) == task_id:
|
||||
self._task_executor_current_task.cancel()
|
||||
self._task_executor_current_task = None
|
||||
return True
|
||||
for task in self._task_executor_queue:
|
||||
if id(task) == task_id:
|
||||
task.cancel()
|
||||
self._task_executor_queue.remove(task)
|
||||
return True
|
||||
raise KeyError(f"Task with ID {task_id} not found in the queue. {self._task_executor_queue=}, {self._task_executor_current_task=}")
|
||||
Reference in new issue
Block a user