Skip to content

Commit 8999366

Browse files
committed
Simplify gz terminate
1 parent 965f115 commit 8999366

4 files changed

Lines changed: 21 additions & 86 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):
Lines changed: 18 additions & 34 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
@@ -9,7 +10,6 @@
910

1011
import logging
1112

12-
1313
class LauncherRos2Api(ILauncher):
1414
type: str
1515
module: str
@@ -43,38 +43,22 @@ def terminate(self):
4343
thread.join()
4444
self.threads.remove(thread)
4545

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-
)
46+
print("Type:", self.type)
47+
48+
to_kill = ["launch.py"]
49+
if self.type == "gz":
50+
to_kill = ["gz", "launch.py"]
51+
else:
52+
to_kill = ["gzserver", "launch.py"]
6353

6454
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-
)
55+
for i in to_kill:
56+
cmd = kill_cmd + i
57+
print(cmd)
58+
subprocess.call(
59+
cmd,
60+
shell=True,
61+
stdout=sys.stdout,
62+
bufsize=1024,
63+
universal_newlines=True,
64+
)

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 & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -652,51 +652,6 @@ def find_docker_console():
652652

653653
LogManager.logger.info("Run application transition finished")
654654

655-
def terminate_harmonic_processes(self):
656-
"""
657-
Terminate all processes within the Docker container whose command line contains 'gz' or 'launch'.
658-
"""
659-
LogManager.logger.info("Terminate Harmonic process")
660-
keywords = ["gz", "launch"]
661-
for keyword in keywords:
662-
try:
663-
ps_aux_cmd = ["ps", "aux"]
664-
grep_cmd = ["grep", keyword]
665-
grep_exclude_cmd = ["grep", "-v", "grep"]
666-
667-
ps_aux_proc = subprocess.Popen(ps_aux_cmd, stdout=subprocess.PIPE)
668-
grep_proc = subprocess.Popen(
669-
grep_cmd, stdin=ps_aux_proc.stdout, stdout=subprocess.PIPE
670-
)
671-
exclude_grep_proc = subprocess.Popen(
672-
grep_exclude_cmd, stdin=grep_proc.stdout, stdout=subprocess.PIPE
673-
)
674-
675-
ps_aux_proc.stdout.close()
676-
grep_proc.stdout.close()
677-
678-
output = exclude_grep_proc.communicate()[0].decode("utf-8")
679-
680-
for line in output.splitlines():
681-
try:
682-
# Extract PID
683-
pid = int(line.split()[1])
684-
subprocess.run(["kill", "-15", str(pid)], check=True)
685-
686-
# Avoid zombies
687-
try:
688-
os.waitpid(pid, 0)
689-
except ChildProcessError:
690-
pass
691-
except Exception as e:
692-
LogManager.logger.exception(
693-
f"Failed to terminate process with line: {line}. Error: {e}"
694-
)
695-
696-
except Exception as e:
697-
LogManager.logger.exception(
698-
f"Failed to search and terminate processes with keyword '{keyword}': {e}"
699-
)
700655

701656
def on_terminate_application(self, event):
702657

@@ -713,15 +668,13 @@ def on_terminate_application(self, event):
713668
def on_terminate_tools(self, event):
714669

715670
self.tools_launcher.terminate()
716-
self.terminate_harmonic_processes()
717671

718672
def on_terminate_universe(self, event):
719673

720674
if self.world_launcher != None:
721675
self.world_launcher.terminate()
722676
if self.robot_launcher != None:
723677
self.robot_launcher.terminate()
724-
self.terminate_harmonic_processes()
725678

726679
def on_disconnect(self, event):
727680

@@ -755,8 +708,6 @@ def on_disconnect(self, event):
755708
except Exception as e:
756709
LogManager.logger.exception("Exception terminating world launcher")
757710

758-
self.terminate_harmonic_processes()
759-
760711
# Reiniciar el script
761712
python = sys.executable
762713
os.execl(python, python, *sys.argv)
@@ -864,7 +815,6 @@ def signal_handler(sign, frame):
864815
except Exception as e:
865816
LogManager.logger.exception("Exception terminating world launcher")
866817

867-
self.terminate_harmonic_processes()
868818
exit()
869819

870820
signal.signal(signal.SIGINT, signal_handler)

0 commit comments

Comments
 (0)