lxc-auto: step by 1 sec up to $max_timeout
authorJohn Audia <redacted>
Sun, 19 Sep 2021 18:35:09 +0000 (14:35 -0400)
committerRosen Penev <redacted>
Sat, 25 Sep 2021 19:37:06 +0000 (12:37 -0700)
If the user defines a $max_timeout of 30, the service will wait 30 seconds
before it considers lxc-stop complete even though lxc-stop might actually
finish much sooner.  This introduces an unneeded delay.

This commit changes the behavior to check once per second to see when lxc-stop
actually stops doing so up to $max_timeout.  It also slightly simplifies the
code with logic to append the -t $max_timeout to the script.

Signed-off-by: John Audia <redacted>
utils/lxc/files/lxc-auto.init

index 1c36483b5c38c1971d22a36c617519e73abf5797..7655a374f0681e72c2c5743eb9d7a88f5126ab8a 100755 (executable)
@@ -35,11 +35,9 @@ stop_container() {
        fi
 
        if [ -n "$name" ]; then
-               if [ "$timeout" = "0" ]; then
-                       /usr/bin/lxc-stop -n "$name" &
-               else
-                       /usr/bin/lxc-stop -n "$name" -t $timeout &
-               fi
+               [ "$timeout" = "0" ] && postargs=" -t $max_timeout"
+               /usr/bin/lxc-stop -n "$name" "$postargs" &
+               export STOPPID=$!
        fi
 }
 
@@ -54,7 +52,13 @@ stop() {
        # ensure e.g. shutdown doesn't occur before maximum timeout on
        # containers that are shutting down
        if [ $max_timeout -gt 0 ]; then
-               sleep $max_timeout
+               for i in $(seq 1 $max_timeout); do
+                       if [ -d /proc/"$STOPPID" ]; then
+                               sleep 1s
+                       else
+                               return 0
+                       fi
+               done
        fi
 }
 
git clone https://git.99rst.org/PROJECT