From d8512957e9b6c772b37dcf7f12ff7f318038e8de Mon Sep 17 00:00:00 2001 From: Leonardo Bergesio Date: Tue, 16 Apr 2024 11:05:45 +0200 Subject: [PATCH] Add signal handling to agbot/css_start.sh When run in a container anax is run via a shell script that does not handle SIGTERM and hence when the container is stopped this signal does not arrive to the binary for graceful exit. A consequence of this is when run via docker compose up, docker compose down will have to wait until the timeout in order to kill the process. The same happens for the ccs-api. This change adds proper SIGTERM handling to agbot_start.sh and css_start.sh. Signed-off-by: Leonardo Bergesio --- anax-in-container/script/agbot_start.sh | 13 +++++++++++-- css/script/css_start.sh | 12 +++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/anax-in-container/script/agbot_start.sh b/anax-in-container/script/agbot_start.sh index 8ece869d6..29c1dfa4d 100755 --- a/anax-in-container/script/agbot_start.sh +++ b/anax-in-container/script/agbot_start.sh @@ -2,10 +2,19 @@ # Script to start agbot from inside the container -if [ -z "$ANAX_LOG_LEVEL" ]; then +if [ -z "${ANAX_LOG_LEVEL}" ]; then ANAX_LOG_LEVEL=3 fi /usr/bin/envsubst < /etc/horizon/anax.json.tmpl > /etc/horizon/anax.json -/usr/horizon/bin/anax -v $ANAX_LOG_LEVEL -logtostderr -config /etc/horizon/anax.json +/usr/horizon/bin/anax -v "${ANAX_LOG_LEVEL}" -logtostderr -config /etc/horizon/anax.json & +ANAX_PID=$! +# If we receive SIGTERM, forward it to anax to start graceful termination +send_sigterm() { + kill "${ANAX_PID}" 2>/dev/null +} +trap send_sigterm TERM + +# Wait for anax termination +wait "${ANAX_PID}" diff --git a/css/script/css_start.sh b/css/script/css_start.sh index fdf41c1ce..a84006e9e 100755 --- a/css/script/css_start.sh +++ b/css/script/css_start.sh @@ -1,4 +1,14 @@ #!/bin/bash /usr/bin/envsubst < /etc/edge-sync-service/sync.conf.tmpl > /etc/edge-sync-service/sync.conf -/home/cssuser/cloud-sync-service +/home/cssuser/cloud-sync-service & +CSS_PID=$! + +# If we receive SIGTERM, forward it to css to start graceful termination +send_sigterm() { + kill "${CSS_PID}" 2>/dev/null +} +trap send_sigterm TERM + +# Wait for css termination +wait "${CSS_PID}"