diff mbox series

[meta-oe] redis: fix service redis-server restart not working under sysvinit

Message ID 20230318001633.18796-1-stefan.ghinea@windriver.com
State Under Review
Headers show
Series [meta-oe] redis: fix service redis-server restart not working under sysvinit | expand

Commit Message

Stefan Ghinea March 18, 2023, 12:16 a.m. UTC
Under sysvinit when trying to restart redis-server using
service redis-server restart two calls are made to start-stop-daemon, first
with the --stop argument and then with --start argument consecutively.

Because the process doesn't immediately terminate when start-stop-daemon
--stop is called, the next call to start-stop-daemon --start finds the
process still running and does not attempt to start another one.

This leads to only a stop of the redis-server process when a restart is
requested. This behavior affects all redis versions using sysvinit only.

This can be fixed by using the --retry <timeout/schedule> argument with
start-stop-daemon --stop in order for the call to block until the process
terminates so that start-stop-daemon --start will attempt to start a new
process.

Unfortunately the --retry argument works only in the implementation of
start-stop-daemon provided by dpkg package and is ignored in the
implementation provided by busybox package.

A repeated check if the process is still running and another try with
another signal after a timeout will effectively simulate a stop with
--retry=TERM/5/KILL/5 schedule.

Signed-off-by: Stefan Ghinea <stefan.ghinea@windriver.com>
---
 .../redis/redis-7/init-redis-server           | 31 +++++++++++++++++++
 .../redis/redis/init-redis-server             | 31 +++++++++++++++++++
 2 files changed, 62 insertions(+)
diff mbox series

Patch

diff --git a/meta-oe/recipes-extended/redis/redis-7/init-redis-server b/meta-oe/recipes-extended/redis/redis-7/init-redis-server
index 6014d70c0..c5f335f57 100755
--- a/meta-oe/recipes-extended/redis/redis-7/init-redis-server
+++ b/meta-oe/recipes-extended/redis/redis-7/init-redis-server
@@ -27,6 +27,37 @@  case "$1" in
     restart)
         echo "Stopping redis-server..."
         start-stop-daemon --stop --quiet --exec /usr/bin/redis-server
+
+        # Since busybox implementation ignores --retry arguments repeatedly check
+        # if the process is still running and try another signal after a timeout,
+        # efectively simulating a stop with --retry=TERM/5/KILL/5 schedule.
+        waitAfterTerm=5000000 # us / 5000 ms / 5 s
+        waitAfterKill=5000000 # us / 5000 ms / 5 s
+        waitStep=100000 # us / 100 ms / 0.1 s
+        waited=0
+        start-stop-daemon --stop --test --quiet --exec /usr/bin/redis-server
+        processOff=$?
+        while [ $processOff -eq 0 ] && [ $waited -le $waitAfterTerm ] ; do
+            usleep ${waitStep}
+            ((waited+=${waitStep}))
+            start-stop-daemon --stop --test --quiet --exec /usr/bin/redis-server
+            processOff=$?
+        done
+        if [ $processOff -eq 0 ] ; then
+            start-stop-daemon --stop --signal KILL --exec /usr/bin/redis-server
+            start-stop-daemon --stop --test --quiet --exec /usr/bin/redis-server
+            processOff=$?
+        fi
+        waited=0
+        while [ $processOff -eq 0 ] && [ $waited -le $waitAfterKill ] ; do
+            usleep ${waitStep}
+            ((waited+=${waitStep}))
+            start-stop-daemon --stop --test --quiet --exec /usr/bin/redis-server
+            processOff=$?
+        done
+        # Here $processOff will indicate if waiting and retrying according to
+        # the schedule ended in a successfull stop or not.
+
 	echo "Starting redis-server..."
         start-stop-daemon --start --quiet --exec /usr/bin/redis-server -- $ARGS
 	;;
diff --git a/meta-oe/recipes-extended/redis/redis/init-redis-server b/meta-oe/recipes-extended/redis/redis/init-redis-server
index 6014d70c0..c5f335f57 100755
--- a/meta-oe/recipes-extended/redis/redis/init-redis-server
+++ b/meta-oe/recipes-extended/redis/redis/init-redis-server
@@ -27,6 +27,37 @@  case "$1" in
     restart)
         echo "Stopping redis-server..."
         start-stop-daemon --stop --quiet --exec /usr/bin/redis-server
+
+        # Since busybox implementation ignores --retry arguments repeatedly check
+        # if the process is still running and try another signal after a timeout,
+        # efectively simulating a stop with --retry=TERM/5/KILL/5 schedule.
+        waitAfterTerm=5000000 # us / 5000 ms / 5 s
+        waitAfterKill=5000000 # us / 5000 ms / 5 s
+        waitStep=100000 # us / 100 ms / 0.1 s
+        waited=0
+        start-stop-daemon --stop --test --quiet --exec /usr/bin/redis-server
+        processOff=$?
+        while [ $processOff -eq 0 ] && [ $waited -le $waitAfterTerm ] ; do
+            usleep ${waitStep}
+            ((waited+=${waitStep}))
+            start-stop-daemon --stop --test --quiet --exec /usr/bin/redis-server
+            processOff=$?
+        done
+        if [ $processOff -eq 0 ] ; then
+            start-stop-daemon --stop --signal KILL --exec /usr/bin/redis-server
+            start-stop-daemon --stop --test --quiet --exec /usr/bin/redis-server
+            processOff=$?
+        fi
+        waited=0
+        while [ $processOff -eq 0 ] && [ $waited -le $waitAfterKill ] ; do
+            usleep ${waitStep}
+            ((waited+=${waitStep}))
+            start-stop-daemon --stop --test --quiet --exec /usr/bin/redis-server
+            processOff=$?
+        done
+        # Here $processOff will indicate if waiting and retrying according to
+        # the schedule ended in a successfull stop or not.
+
 	echo "Starting redis-server..."
         start-stop-daemon --start --quiet --exec /usr/bin/redis-server -- $ARGS
 	;;