From b824c2f5bd6d52f004839b0c26028fe586b1ecea Mon Sep 17 00:00:00 2001 From: Tomoya Fujita Date: Sat, 4 May 2024 10:15:18 -0700 Subject: [PATCH] add signal handler for SIGINT and SIGTERM. (#29) * add signal handler for SIGINT and SIGTERM. Signed-off-by: Tomoya Fujita * doc update for pip install. Signed-off-by: Tomoya Fujita * adapt pip install option based on system and distro. Signed-off-by: Tomoya Fujita --------- Signed-off-by: Tomoya Fujita --- README.md | 8 ++++++++ docker/Dockerfile | 10 +++++++++- ros2ai/api/utils.py | 13 +++++++++++++ scripts/github_workflows.sh | 14 +++++++++++++- 4 files changed, 43 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4664745..c9b18cf 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,8 @@ https://github.com/fujitatomoya/ros2ai/assets/43395114/2af4fd44-2ccf-472c-9153-c ### Required Package +- `rolling` / `jazzy` + ```bash pip install openai --break-system-packages ``` @@ -54,6 +56,12 @@ pip install openai --break-system-packages > [!NOTE] > see [PEP 668 – Marking Python base environments as “externally managed”](PEP 668 – Marking Python base environments as “externally managed”) why `--break-system-packages` is required. +- `iron` / `humble` + +```bash +pip install openai +``` + ### Build No released package is available, needs to be build in colcon workspace. diff --git a/docker/Dockerfile b/docker/Dockerfile index 46669ea..4a03a82 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -36,7 +36,15 @@ RUN apt-get update \ pip curl\ --no-install-recommends \ && rm -rf /var/lib/apt/lists/* -RUN pip install openai + +# Adapt pip install option based on distro (technically ubuntu version) +RUN if [ "$ROS_DISTRO" = "humble" ]; then \ + pip install openai; \ + elif [ "$ROS_DISTRO" = "iron" ]; then \ + pip install openai; \ + else \ + pip install openai --break-system-packages; \ + fi # Build and source colcon workspace RUN cd $COLCON_WS \ diff --git a/ros2ai/api/utils.py b/ros2ai/api/utils.py index e83839b..3e0ad3b 100644 --- a/ros2ai/api/utils.py +++ b/ros2ai/api/utils.py @@ -48,6 +48,19 @@ def run_executable(*, command, argv = None, prefix=None): command = prefix + command process = subprocess.Popen(command, shell = True) + + # add signal handler for the parent process, so that we can finalize the child process + # child process could be `ros2 run` process and that also should pass the signal to + # executables running underneath. finally executables can handle the signals. + def signal_handler(sig, frame): + print('[ros2ai]:', 'Received signal: ', signal.strsignal(sig)) + if process.poll() is None: + # If child process is running, forward the signal to it + process.send_signal(sig) + + signal.signal(signal.SIGINT, signal_handler) + signal.signal(signal.SIGTERM, signal_handler) + while process.returncode is None: try: process.communicate() diff --git a/scripts/github_workflows.sh b/scripts/github_workflows.sh index f9afc4c..e633ca1 100755 --- a/scripts/github_workflows.sh +++ b/scripts/github_workflows.sh @@ -27,13 +27,23 @@ function exit_trap() { fi } +function get_ubuntu_version () { + trap exit_trap ERR + echo "[${FUNCNAME[0]}]: get ubuntu version." + UBUNTU_VERSION=$(grep '^VERSION_ID=' /etc/os-release | awk -F'=' '{print $2}' | tr -d '"') +} + function install_prerequisites () { trap exit_trap ERR echo "[${FUNCNAME[0]}]: update and install dependent packages." apt update && apt upgrade -y # TODO@fujitatomoya: should install openai via package.xml apt install -y pip - pip install openai --break-system-packages + if [ $UBUNTU_VERSION == "24.04" ]; then + pip install openai --break-system-packages + else + pip install openai + fi #apt install -y ros-${ROS_DISTRO}-desktop --no-install-recommends cd $there } @@ -61,6 +71,7 @@ function build_colcon_package () { export DEBIAN_FRONTEND=noninteractive export COLCON_WORKSPACE=/tmp/colcon_ws +export UBUNTU_VERSION=24.04 # mark the working space root directory, so that we can come back anytime with `cd $there` mark there @@ -69,6 +80,7 @@ mark there trap exit_trap ERR # call install functions in sequence +get_ubuntu_version install_prerequisites setup_build_colcon_env build_colcon_package