Skip to content

Commit d2f3388

Browse files
joaomariolagopatrickelectric
authored andcommitted
services: ardupilot-manager: Avoid double starts
* Avoid autopilot manager to start when already running
1 parent fb65594 commit d2f3388

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

core/services/ardupilot_manager/autopilot_manager.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ async def setup(self) -> None:
5757
# This is the logical continuation of __init__(), extracted due to its async nature
5858
self.configuration = deepcopy(self.settings.content)
5959

60+
# Undesired state, only to avoid losing the reference to a running MavlinkManager
61+
if self.mavlink_manager is not None:
62+
await self.mavlink_manager.stop()
63+
6064
self.mavlink_manager = MavlinkManager()
6165
preferred_router = self.load_preferred_router()
6266
try:
@@ -101,16 +105,24 @@ def need_to_remove_file(file: pathlib.Path) -> bool:
101105
except Exception as error:
102106
logger.warning(f"Failed to remove logs: {error}")
103107

108+
def is_running(self) -> bool:
109+
if self.current_board is None:
110+
return False
111+
112+
if self.current_board.type in [PlatformType.SITL, PlatformType.Linux]:
113+
return (
114+
self.ardupilot_subprocess is not None
115+
and self.ardupilot_subprocess.poll() is None
116+
and len(self.running_ardupilot_processes()) != 0
117+
)
118+
119+
# Serial or others that are not processes based
120+
return self.should_be_running
121+
104122
async def auto_restart_ardupilot(self) -> None:
105123
"""Auto-restart Ardupilot when it's not running but was supposed to."""
106124
while True:
107-
process_not_running = (
108-
self.ardupilot_subprocess is not None and self.ardupilot_subprocess.poll() is not None
109-
) or len(self.running_ardupilot_processes()) == 0
110-
needs_restart = self.should_be_running and (
111-
self.current_board is None
112-
or (self.current_board.type in [PlatformType.SITL, PlatformType.Linux] and process_not_running)
113-
)
125+
needs_restart = self.should_be_running and not self.is_running()
114126
if needs_restart:
115127
logger.debug("Restarting ardupilot...")
116128
try:
@@ -549,6 +561,10 @@ async def kill_ardupilot(self) -> None:
549561
logger.info("Mavlink manager stopped.")
550562

551563
async def start_ardupilot(self) -> None:
564+
# This only applies to autopilot process itself, mavlink manager will check by itself
565+
if self.should_be_running and self.is_running():
566+
return
567+
552568
await self.setup()
553569
try:
554570
available_boards = await self.available_boards()

0 commit comments

Comments
 (0)