fix: Handle terminate signals in init script

`tini` does not wait for child processes to close, so all processes will be killed immediately. This is why the container stops so fast.

This fix makes the `init` script listen and handle terminate signals. It also ensures that child processes are shut down in reverse order with proper waiting for completion.
This commit is contained in:
Mikhail Vazhnov
2024-12-26 08:08:25 +03:00
parent f3c5e82aa9
commit f5bd75cced
2 changed files with 33 additions and 7 deletions

View File

@@ -120,7 +120,6 @@ RUN apk add --no-cache \
mariadb-connector-c \
p7zip \
python3 \
tini \
tzdata \
valkey
@@ -152,5 +151,5 @@ VOLUME ["/romm/resources", "/romm/library", "/romm/assets", "/romm/config", "/re
EXPOSE 8080 6379/tcp
WORKDIR /romm
ENTRYPOINT ["/sbin/tini", "--", "/docker-entrypoint.sh"]
ENTRYPOINT ["/docker-entrypoint.sh"]
CMD ["/init"]

View File

@@ -115,19 +115,43 @@ watchdog_process_pid() {
fi
}
stop_process_pid() {
PROCESS=$1
if [[ -f "/tmp/${PROCESS}.pid" ]]; then
PID=$(cat "/tmp/${PROCESS}.pid") || true
if [[ -d "/proc/${PID}" ]]; then
info_log "stopping ${PROCESS}"
kill "${PID}" || true
# wait for process exit
while [ -e "/proc/${PID}" ]; do sleep 0.1; done
fi
fi
}
shutdown() {
# shutdown in reverse order
stop_process_pid scheduler
stop_process_pid worker
stop_process_pid watcher
stop_process_pid nginx
stop_process_pid gunicorn
stop_process_pid valkey-server
}
# switch to backend directory
cd /backend || { error_log "/backend directory doesn't seem to exist"; }
info_log "Starting up, please wait..."
# setup trap handler
exited=0
trap 'exited=1 && shutdown' SIGINT SIGTERM EXIT
# clear any leftover PID files
rm /tmp/*.pid -f
# function definition done, lets start our main loop
while true; do
# check for died processes every 5 seconds
sleep 5
while ! ((exited)); do
# Start Valkey server if we dont have a corresponding PID file
# and REDIS_HOST is not set (which would mean we're using an external Redis/Valkey)
if [[ -z ${REDIS_HOST:=""} ]]; then
@@ -138,7 +162,7 @@ while true; do
# but only if it was not successful since the last full docker container start
if [[ ${ALEMBIC_SUCCESS:="false"} == "false" ]]; then
if alembic upgrade head; then
debug_log "database schema migrations suceeded"
debug_log "database schema migrations succeeded"
ALEMBIC_SUCCESS="true"
else
error_log "Something went horribly wrong with our database"
@@ -165,4 +189,7 @@ while true; do
watchdog_process_pid python worker
# Start scheduler if we dont have a corresponding PID file
watchdog_process_pid python scheduler
# check for died processes every 5 seconds
sleep 5
done