Skip to content

Commit 3052660

Browse files
authored
Cancel Mission on STOP (#18)
1 parent a524e44 commit 3052660

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

task_manager/task_manager/tasks/mission.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,17 @@ def execute_cb(self, goal_handle: ServerGoalHandle) -> MissionAction.Result:
8181
mission_result.skipped = False
8282

8383
if subtask_result.task_status != TaskStatus.DONE:
84-
if subtask.allow_skipping:
84+
if subtask.allow_skipping and not goal_handle.is_cancel_requested:
8585
mission_result.skipped = True
8686
continue
8787

88+
# If the subtask has been cancelled together with the mission, we cancel the mission
89+
# If the subtask has been cancelled/failed/indefinitely in progress, we abort the mission
90+
# If the mission has been cancelled but the subtask is failed/indefinitely in progress,
91+
# we abort the mission
8892
if subtask_result.task_status == TaskStatus.CANCELED and goal_handle.is_cancel_requested:
89-
# Need to also check if the cancel was requested. If the goal was cancelled
90-
# through a system task, we cannot set the status to be cancelled and must abort instead.
9193
goal_handle.canceled()
92-
else: # Could be ERROR or IN_PROGRESS if the goal couldn't be cancelled
94+
else:
9395
goal_handle.abort()
9496
return result
9597

@@ -102,7 +104,7 @@ def get_task_specs(mission_topic) -> TaskSpecs:
102104
return TaskSpecs(
103105
task_name="system/mission",
104106
blocking=False,
105-
cancel_on_stop=False,
107+
cancel_on_stop=True,
106108
topic=mission_topic,
107109
cancel_reported_as_success=False,
108110
reentrant=False,

task_manager/test/test_mission.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,33 @@ def test_mission_not_successful(self):
8484
mock_goal_handle.abort.assert_called_once()
8585
self.assertEqual(result.mission_results[0].task_status, TaskStatus.ERROR)
8686

87+
def test_mission_not_successful_skipping_task(self):
88+
"""Tests that the mission is properly cancelled or aborted when a subtask is skipped."""
89+
request = MissionAction.Goal(
90+
subtasks=[SubtaskGoal(task_name="test/mock_subtask", allow_skipping=True, task_data="{}")]
91+
)
92+
mock_goal_handle = Mock(request=request)
93+
mock_goal_handle.is_cancel_requested = True
94+
95+
with self.subTest("mission_canceled"):
96+
self.mission.execute_task_cb.return_value = ExecuteTask.Result(
97+
task_status=TaskStatus.CANCELED, task_result="{}"
98+
)
99+
result = self.mission.execute_cb(goal_handle=mock_goal_handle)
100+
mock_goal_handle.canceled.assert_called_once()
101+
self.assertEqual(result.mission_results[0].task_status, TaskStatus.CANCELED)
102+
103+
mock_goal_handle.reset_mock()
104+
with self.subTest("mission_error"):
105+
self.mission.execute_task_cb.return_value = ExecuteTask.Result(
106+
task_status=TaskStatus.ERROR, task_result="{}"
107+
)
108+
result = self.mission.execute_cb(goal_handle=mock_goal_handle)
109+
mock_goal_handle.abort.assert_called_once()
110+
self.assertEqual(result.mission_results[0].task_status, TaskStatus.ERROR)
111+
87112
def test_skipping_subtask(self):
88-
"""Tests that even tho a subtask is aborted, no error is raised when the task is allowed to be skipped."""
113+
"""Tests that even though a subtask is aborted, no error is raised when the task is allowed to be skipped."""
89114
request = MissionAction.Goal()
90115
request.subtasks = [
91116
SubtaskGoal(task_name="test/mock_subtask", task_data="{}", allow_skipping=True, task_id="123")
@@ -97,8 +122,9 @@ def test_skipping_subtask(self):
97122
expected_result.mission_results = [
98123
SubtaskResult(task_name="test/mock_subtask", task_status=TaskStatus.ERROR, skipped=True, task_id="123")
99124
]
100-
101-
result = self.mission.execute_cb(goal_handle=Mock(request=request))
125+
mock_goal_handle = Mock(request=request)
126+
mock_goal_handle.is_cancel_requested = False
127+
result = self.mission.execute_cb(goal_handle=mock_goal_handle)
102128
self.assertEqual(result, expected_result)
103129

104130

0 commit comments

Comments
 (0)