Skip to content

Commit

Permalink
Use tenacity for retries
Browse files Browse the repository at this point in the history
  • Loading branch information
b-Tomas committed Feb 5, 2025
1 parent 5de9bac commit c0ea0cc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
19 changes: 7 additions & 12 deletions mir_connector/inorbit_mir_connector/src/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import pytz
import math
import uuid
import logging
from tenacity import retry, wait_exponential_jitter, before_sleep_log
from threading import Thread
from inorbit_connector.connector import Connector
from inorbit_edge.robot import COMMAND_CUSTOM_COMMAND
Expand Down Expand Up @@ -97,9 +99,7 @@ def __init__(self, robot_id: str, config: MiR100Config) -> None:
)

# Get or create the required missions and mission groups
Thread(
target=self.retry_until_successful, args=(self.setup_connector_missions,), daemon=True
).start()
Thread(target=self.setup_connector_missions, daemon=True).start()

def _inorbit_command_handler(self, command_name, args, options):
"""Callback method for command messages.
Expand Down Expand Up @@ -357,6 +357,10 @@ def send_waypoint_over_missions(self, pose):
)
self.mir_api.queue_mission(mission_id)

@retry(
wait=wait_exponential_jitter(max=10),
before_sleep=before_sleep_log(logging.getLogger(__name__), logging.WARNING),
)
def setup_connector_missions(self):
"""Find and store the required missions and mission groups, or create them if they don't
exist."""
Expand All @@ -383,15 +387,6 @@ def setup_connector_missions(self):
f"guid '{self.tmp_missions_group_id}'"
)

def retry_until_successful(self, func, *args, **kwargs):
"""Retry a function until it is successful"""
while True:
try:
return func(*args, **kwargs)
except Exception as ex:
self._logger.error(f"Failed to execute function: {ex}")
sleep(5)

def cleanup_connector_missions(self):
"""Delete the missions group created at startup"""
self._logger.info("Cleaning up connector missions")
Expand Down
1 change: 1 addition & 0 deletions mir_connector/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"psutil==5.9",
"websocket-client==1.7.0",
"uuid==1.30",
"tenacity==9.0.0",
]

test_requirements = [
Expand Down

0 comments on commit c0ea0cc

Please sign in to comment.