Skip to content

Commit dcce1e7

Browse files
authored
Merge pull request #236 from JdeRobot/simplify-harmonic-terminate
Simplify gz terminate
2 parents 9f140fd + d7f4a56 commit dcce1e7

5 files changed

Lines changed: 30 additions & 100 deletions

File tree

manager/manager/launcher/launcher_gzsim.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class LauncherGzsim(ILauncher):
6060
width: int
6161
running: bool = False
6262
threads: List[Any] = []
63+
acceptsMsgs: bool = False
6364
gz_vnc: Any = Vnc_server()
6465

6566
def run(self, config_file, callback):

manager/manager/launcher/launcher_ros.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ class LauncherRos(ILauncher):
4444
def run(self):
4545
try:
4646
# generate entry_point environment variable
47-
os.environ[
48-
"EXERCISE_FOLDER"
49-
] = f"{os.environ.get('EXERCISES_STATIC_FOLDER')}/{self.exercise_id}"
47+
os.environ["EXERCISE_FOLDER"] = (
48+
f"{os.environ.get('EXERCISES_STATIC_FOLDER')}/{self.exercise_id}"
49+
)
5050

5151
# expand variables in configuration paths
5252
resource_folders = [
@@ -57,15 +57,15 @@ def run(self):
5757
launch_file = os.path.expandvars(self.launch_file)
5858

5959
env = dict(os.environ)
60-
env[
61-
"GAZEBO_RESOURCE_PATH"
62-
] = f"{env.get('GAZEBO_RESOURCE_PATH', '')}:{':'.join(resource_folders)}"
63-
env[
64-
"GAZEBO_MODEL_PATH"
65-
] = f"{env.get('GAZEBO_MODEL_PATH', '')}:{':'.join(model_folders)}"
66-
env[
67-
"GAZEBO_PLUGIN_PATH"
68-
] = f"{env.get('GAZEBO_PLUGIN_PATH', '')}:{':'.join(plugin_folders)}"
60+
env["GAZEBO_RESOURCE_PATH"] = (
61+
f"{env.get('GAZEBO_RESOURCE_PATH', '')}:{':'.join(resource_folders)}"
62+
)
63+
env["GAZEBO_MODEL_PATH"] = (
64+
f"{env.get('GAZEBO_MODEL_PATH', '')}:{':'.join(model_folders)}"
65+
)
66+
env["GAZEBO_PLUGIN_PATH"] = (
67+
f"{env.get('GAZEBO_PLUGIN_PATH', '')}:{':'.join(plugin_folders)}"
68+
)
6969

7070
parameters = " ".join(self.parameters)
7171
command = f"{self.ros_command_line} {parameters} {launch_file}"

manager/manager/launcher/launcher_ros2_api.py

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import sys
23
from typing import List, Any
34
import time
45
import stat
@@ -43,38 +44,19 @@ def terminate(self):
4344
thread.join()
4445
self.threads.remove(thread)
4546

46-
kill_cmd = "pkill -9 -f "
47-
cmd = kill_cmd + "gzserver"
48-
subprocess.call(
49-
cmd,
50-
shell=True,
51-
stdout=subprocess.PIPE,
52-
bufsize=1024,
53-
universal_newlines=True,
54-
)
55-
cmd = kill_cmd + "spawn_model.launch.py"
56-
subprocess.call(
57-
cmd,
58-
shell=True,
59-
stdout=subprocess.PIPE,
60-
bufsize=1024,
61-
universal_newlines=True,
62-
)
47+
to_kill = ["launch.py"]
48+
if self.type == "gz":
49+
to_kill = ["gz", "launch.py"]
50+
else:
51+
to_kill = ["gzserver", "launch.py"]
6352

6453
kill_cmd = "pkill -9 -f "
65-
cmd = kill_cmd + "gzserver"
66-
subprocess.call(
67-
cmd,
68-
shell=True,
69-
stdout=subprocess.PIPE,
70-
bufsize=1024,
71-
universal_newlines=True,
72-
)
73-
cmd = kill_cmd + "spawn_model.launch.py"
74-
subprocess.call(
75-
cmd,
76-
shell=True,
77-
stdout=subprocess.PIPE,
78-
bufsize=1024,
79-
universal_newlines=True,
80-
)
54+
for i in to_kill:
55+
cmd = kill_cmd + i
56+
subprocess.call(
57+
cmd,
58+
shell=True,
59+
stdout=subprocess.PIPE,
60+
bufsize=1024,
61+
universal_newlines=True,
62+
)

manager/manager/launcher/launcher_world.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"gazebo": {
1010
"2": [
1111
{
12-
"type": "module",
12+
"type": "gazebo",
1313
"module": "ros2_api",
1414
"parameters": [],
1515
"launch_file": [],
@@ -19,7 +19,7 @@
1919
"gz": {
2020
"2": [
2121
{
22-
"type": "module",
22+
"type": "gz",
2323
"module": "ros2_api",
2424
"parameters": [],
2525
"launch_file": [],

manager/manager/manager.py

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -718,54 +718,6 @@ def find_docker_console():
718718

719719
LogManager.logger.info("Run application transition finished")
720720

721-
def terminate_harmonic_processes(self):
722-
"""
723-
Terminate all Harmonic processes in the container.
724-
725-
Terminate all processes in the container
726-
whose command line contains 'gz' or 'launch'.
727-
"""
728-
LogManager.logger.info("Terminate Harmonic process")
729-
keywords = ["gz", "launch"]
730-
for keyword in keywords:
731-
try:
732-
ps_aux_cmd = ["ps", "aux"]
733-
grep_cmd = ["grep", keyword]
734-
grep_exclude_cmd = ["grep", "-v", "grep"]
735-
736-
ps_aux_proc = subprocess.Popen(ps_aux_cmd, stdout=subprocess.PIPE)
737-
grep_proc = subprocess.Popen(
738-
grep_cmd, stdin=ps_aux_proc.stdout, stdout=subprocess.PIPE
739-
)
740-
exclude_grep_proc = subprocess.Popen(
741-
grep_exclude_cmd, stdin=grep_proc.stdout, stdout=subprocess.PIPE
742-
)
743-
744-
ps_aux_proc.stdout.close()
745-
grep_proc.stdout.close()
746-
747-
output = exclude_grep_proc.communicate()[0].decode("utf-8")
748-
749-
for line in output.splitlines():
750-
try:
751-
# Extract PID
752-
pid = int(line.split()[1])
753-
subprocess.run(["kill", "-15", str(pid)], check=True)
754-
755-
# Avoid zombies
756-
try:
757-
os.waitpid(pid, 0)
758-
except ChildProcessError:
759-
pass
760-
except Exception as e:
761-
LogManager.logger.exception(
762-
f"Failed to terminate process with line: {line}. Error: {e}"
763-
)
764-
765-
except Exception as e:
766-
LogManager.logger.exception(
767-
f"Failed to search and terminate processes with keyword '{keyword}': {e}"
768-
)
769721

770722
def on_terminate_application(self, event):
771723
"""
@@ -790,7 +742,6 @@ def on_terminate_application(self, event):
790742
def on_terminate_tools(self, event):
791743

792744
self.tools_launcher.terminate()
793-
self.terminate_harmonic_processes()
794745

795746
def on_terminate_universe(self, event):
796747
"""
@@ -806,7 +757,6 @@ def on_terminate_universe(self, event):
806757
self.world_launcher.terminate()
807758
if self.robot_launcher is not None:
808759
self.robot_launcher.terminate()
809-
self.terminate_harmonic_processes()
810760

811761
def on_disconnect(self, event):
812762
"""
@@ -846,8 +796,6 @@ def on_disconnect(self, event):
846796
except Exception as e:
847797
LogManager.logger.exception("Exception terminating world launcher")
848798

849-
self.terminate_harmonic_processes()
850-
851799
# Reiniciar el script
852800
python = sys.executable
853801
os.execl(python, python, *sys.argv)
@@ -970,7 +918,6 @@ def signal_handler(sign, frame):
970918
except Exception as e:
971919
LogManager.logger.exception("Exception terminating world launcher")
972920

973-
self.terminate_harmonic_processes()
974921
exit()
975922

976923
signal.signal(signal.SIGINT, signal_handler)

0 commit comments

Comments
 (0)