Skip to content

Commit

Permalink
Retry on failed attempt not on queue loop
Browse files Browse the repository at this point in the history
  • Loading branch information
aeshub committed Nov 23, 2022
1 parent b35c3c1 commit fa634f3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
9 changes: 6 additions & 3 deletions src/isar/state_machine/states/stop_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,16 @@ def _run(self):
try:
self.stop_thread.get_output()
except ThreadedRequestNotFinishedError:
time.sleep(self.state_machine.sleep_time)
continue

except RobotException:
if self.handle_stop_fail(
retry_limit=self.state_machine.stop_robot_attempts_limit
):
transition = self.state_machine.mission_stopped
break
continue

except RobotException:
self.logger.warning("Failed to stop robot. Retrying.")
self.stop_thread = None
continue
Expand All @@ -68,7 +70,8 @@ def handle_stop_fail(self, retry_limit: int) -> bool:
self._count_number_retries += 1
if self._count_number_retries > retry_limit:
self.logger.warning(
"Could not communicate request: Reached limit for stop attemps. Cancelled mission and transitioned to idle."
"Could not communicate request: Reached limit for stop attempts. "
"Cancelled mission and transitioned to idle."
)
return True
time.sleep(self.state_machine.sleep_time)
Expand Down
17 changes: 11 additions & 6 deletions tests/isar/state_machine/test_state_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
from typing import List

import pytest
import logging

from injector import Injector
from pytest_mock import MockerFixture

from isar.mission_planner.local_planner import LocalPlanner
from isar.models.communication.queues.queues import Queues
Expand All @@ -18,12 +17,12 @@
from isar.state_machine.states_enum import States
from isar.storage.storage_interface import StorageInterface
from isar.storage.uploader import Uploader
from robot_interface.models.exceptions import RobotException
from robot_interface.models.mission import DriveToPose, Step, TakeImage
from robot_interface.models.mission.status import StepStatus
from tests.mocks.pose import MockPose
from tests.mocks.robot_interface import MockRobot
from tests.mocks.step import MockStep
from pytest_mock import MockerFixture


class StateMachineThread(object):
Expand Down Expand Up @@ -214,7 +213,10 @@ def test_state_machine_with_successful_mission_stop(
]
)
actual = state_machine_thread.state_machine.transitions_list
unexpected_log = "Could not communicate request: Reached limit for stop attemps. Cancelled mission and transitioned to idle."
unexpected_log = (
"Could not communicate request: Reached limit for stop attempts. "
"Cancelled mission and transitioned to idle."
)
assert unexpected_log not in caplog.text
assert expected == actual

Expand All @@ -229,7 +231,7 @@ def test_state_machine_with_unsuccsessful_mission_stop(
mission: Mission = Mission(tasks=[Task(steps=[step])])
scheduling_utilities: SchedulingUtilities = injector.get(SchedulingUtilities)
scheduling_utilities.start_mission(mission=mission, initial_pose=None)
mocker.patch.object(ThreadedRequest, "_is_thread_alive", return_value=True)
mocker.patch.object(ThreadedRequest, "get_output", side_effect=RobotException)
scheduling_utilities.stop_mission()
expected = deque(
[
Expand All @@ -241,6 +243,9 @@ def test_state_machine_with_unsuccsessful_mission_stop(
]
)
actual = state_machine_thread.state_machine.transitions_list
expected_log = "Could not communicate request: Reached limit for stop attemps. Cancelled mission and transitioned to idle."
expected_log = (
"Could not communicate request: Reached limit for stop attempts. "
"Cancelled mission and transitioned to idle."
)
assert expected_log in caplog.text
assert expected == actual

0 comments on commit fa634f3

Please sign in to comment.