Skip to content

Commit

Permalink
add signal handler for SIGINT and SIGTERM. (#29)
Browse files Browse the repository at this point in the history
* add signal handler for SIGINT and SIGTERM.

Signed-off-by: Tomoya Fujita <[email protected]>

* doc update for pip install.

Signed-off-by: Tomoya Fujita <[email protected]>

* adapt pip install option based on system and distro.

Signed-off-by: Tomoya Fujita <[email protected]>

---------

Signed-off-by: Tomoya Fujita <[email protected]>
  • Loading branch information
fujitatomoya committed May 4, 2024
1 parent d365c3c commit b824c2f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,21 @@ https://github.com/fujitatomoya/ros2ai/assets/43395114/2af4fd44-2ccf-472c-9153-c

### Required Package

- `rolling` / `jazzy`

```bash
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.
Expand Down
10 changes: 9 additions & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
13 changes: 13 additions & 0 deletions ros2ai/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
14 changes: 13 additions & 1 deletion scripts/github_workflows.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit b824c2f

Please sign in to comment.