Skip to content

Commit

Permalink
fix: improve launch_with_restart_trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
MrBlenny committed Jan 29, 2024
1 parent 063bfc5 commit 0084506
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions launch_ext/entrypoints/launch_with_restart_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class SharedState:
def __init__(self):
self.launch_service = LaunchService()
self.restarted_via_trigger = False
self.launch_terminated = False

class TriggerNode(Node):
def __init__(
Expand All @@ -24,11 +25,9 @@ def __init__(
node_name: str,
trigger_name: str,
shared_state: SharedState,
sleep_time: float = 1.0
):
super().__init__(node_name, namespace=namespace)
self.shared_state = shared_state
self.sleep_time = sleep_time
self.srv = self.create_service(Trigger, 'restart', self.trigger_callback)
logger.info(f"Enabling restart for launch_service - {namespace}/{trigger_name}")

Expand All @@ -38,12 +37,19 @@ def trigger_callback(self, request: Trigger_Request, response: Trigger_Response)
self.shared_state.restarted_via_trigger = True
self.shared_state.launch_service.shutdown()

logger.info(f"Restart launch_service completed")
time.sleep(self.sleep_time)
response.success = True

# Reset the flag
self.shared_state.restarted_via_trigger = False
# Wait for the launch to terminate
while not self.shared_state.launch_terminated:
time.sleep(0.1)

logger.info(f"Restart launch_service completed")
response.success = True

# Reset the flag
self.shared_state.restarted_via_trigger = False

else:
logger.info(f"Launch service not yet started")
response.success = False

return response

Expand Down Expand Up @@ -83,7 +89,6 @@ def run_ros(shared_state: SharedState):
node_name=node_name,
trigger_name=trigger_name,
shared_state=shared_state,
sleep_time=sleep_time
)
rclpy.spin(node)
except KeyboardInterrupt:
Expand All @@ -94,12 +99,14 @@ def run_ros(shared_state: SharedState):
def run_launch(shared_state: SharedState):
while True:
logger.info("Running launch service...")
shared_state.launch_terminated = False
shared_state.launch_service = LaunchService()
shared_state.launch_service.include_launch_description(generate_launch_description())
shared_state.launch_service.run()
if (shared_state.restarted_via_trigger):
logger.info("Launch shutdown due to trigger. Restarting...")
time.sleep(1)
logger.info(f"Launch shutdown due to trigger. Restarting in {sleep_time}s...")
time.sleep(sleep_time)
shared_state.launch_terminated = True
continue
else:
logger.info("Launch shutdown due to user.")
Expand Down

0 comments on commit 0084506

Please sign in to comment.