Skip to content

Commit

Permalink
services: ardupilot-manager: Avoid double starts
Browse files Browse the repository at this point in the history
* Avoid autopilot manager to start when already running
  • Loading branch information
JoaoMario109 committed Jan 13, 2025
1 parent 86f03b3 commit 3982dcc
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions core/services/ardupilot_manager/autopilot_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ async def setup(self) -> None:
# This is the logical continuation of __init__(), extracted due to its async nature
self.configuration = deepcopy(self.settings.content)

# Undesired state, only to avoid losing the reference to a running MavlinkManager
if self.mavlink_manager is not None:
await self.mavlink_manager.stop()
self.mavlink_manager = None

self.mavlink_manager = MavlinkManager()
preferred_router = self.load_preferred_router()
try:
Expand Down Expand Up @@ -101,16 +106,24 @@ def need_to_remove_file(file: pathlib.Path) -> bool:
except Exception as error:
logger.warning(f"Failed to remove logs: {error}")

def is_running(self) -> bool:
if self.current_board is None:
return False

if self.current_board.type in [PlatformType.SITL, PlatformType.Linux]:
return (
self.ardupilot_subprocess is not None
and self.ardupilot_subprocess.poll() is None
and len(self.running_ardupilot_processes()) != 0
)

# Serial or others that are not processes based
return self.should_be_running

async def auto_restart_ardupilot(self) -> None:
"""Auto-restart Ardupilot when it's not running but was supposed to."""
while True:
process_not_running = (
self.ardupilot_subprocess is not None and self.ardupilot_subprocess.poll() is not None
) or len(self.running_ardupilot_processes()) == 0
needs_restart = self.should_be_running and (
self.current_board is None
or (self.current_board.type in [PlatformType.SITL, PlatformType.Linux] and process_not_running)
)
needs_restart = self.should_be_running and not self.is_running()
if needs_restart:
logger.debug("Restarting ardupilot...")
try:
Expand Down Expand Up @@ -549,6 +562,10 @@ async def kill_ardupilot(self) -> None:
logger.info("Mavlink manager stopped.")

async def start_ardupilot(self) -> None:
# This only applies to autopilot process itself, mavlink manager will check by itself
if self.should_be_running and self.is_running():
return

await self.setup()
try:
available_boards = await self.available_boards()
Expand Down

0 comments on commit 3982dcc

Please sign in to comment.