From 534a9db6fdebc3b1b933144e9932b69753fda3af Mon Sep 17 00:00:00 2001 From: Lucas Saldanha Date: Mon, 17 Oct 2016 23:56:05 +1300 Subject: [PATCH] Make stop wait time in the launch script configurable Create a parameter `STOP_WAIT_TIME` for the startup script that configures the time in seconds to wait for a normal shutdown. Because of #4941 we also send a shutdown half way the countdown. Fixes gh-7121 --- spring-boot-docs/src/main/asciidoc/deployment.adoc | 8 ++++++++ .../springframework/boot/loader/tools/launch.script | 7 +++++-- .../boot/loader/tools/DefaultLaunchScriptTests.java | 12 ++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/spring-boot-docs/src/main/asciidoc/deployment.adoc b/spring-boot-docs/src/main/asciidoc/deployment.adoc index 714efaf4ceb..c8aa0fc6819 100644 --- a/spring-boot-docs/src/main/asciidoc/deployment.adoc +++ b/spring-boot-docs/src/main/asciidoc/deployment.adoc @@ -634,6 +634,10 @@ for Gradle and to `${project.name}` for Maven. |`useStartStopDaemon` |If the `start-stop-daemon` command, when it's available, should be used to control the process. Defaults to `true`. + +|`stopWaitTime` +|The default value for `STOP_WAIT_TIME`. Only valid for an `init.d` service. + Defaults to 60 seconds. |=== @@ -694,6 +698,10 @@ The following environment properties are supported with the default script: |`DEBUG` |if not empty will set the `-x` flag on the shell process, making it easy to see the logic in the script. + +|`STOP_WAIT_TIME` +|The time in seconds to wait when stopping the application before forcing a shutdown + (`60` by default). |=== NOTE: The `PID_FOLDER`, `LOG_FOLDER` and `LOG_FILENAME` variables are only valid for an diff --git a/spring-boot-tools/spring-boot-loader-tools/src/main/resources/org/springframework/boot/loader/tools/launch.script b/spring-boot-tools/spring-boot-loader-tools/src/main/resources/org/springframework/boot/loader/tools/launch.script index 84cadd780af..feb9f538a70 100755 --- a/spring-boot-tools/spring-boot-loader-tools/src/main/resources/org/springframework/boot/loader/tools/launch.script +++ b/spring-boot-tools/spring-boot-loader-tools/src/main/resources/org/springframework/boot/loader/tools/launch.script @@ -73,6 +73,9 @@ fi # Initialize log file name if not provided by the config file [[ -z "$LOG_FILENAME" ]] && LOG_FILENAME="{{logFilename:${identity}.log}}" +# Initialize stop wait time if not provided by the config file +[[ -z "$STOP_WAIT_TIME" ]] && STOP_WAIT_TIME={{stopWaitTime:60}} + # ANSI Colors echoRed() { echo $'\e[0;31m'"$1"$'\e[0m'; } echoGreen() { echo $'\e[0;32m'"$1"$'\e[0m'; } @@ -191,9 +194,9 @@ stop() { do_stop() { kill "$1" &> /dev/null || { echoRed "Unable to kill process $1"; return 1; } - for i in $(seq 1 60); do + for i in $(seq 1 $STOP_WAIT_TIME); do isRunning "$1" || { echoGreen "Stopped [$1]"; rm -f "$2"; return 0; } - [[ $i -eq 30 ]] && kill "$1" &> /dev/null + [[ $i -eq STOP_WAIT_TIME/2 ]] && kill "$1" &> /dev/null sleep 1 done echoRed "Unable to kill process $1"; diff --git a/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/DefaultLaunchScriptTests.java b/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/DefaultLaunchScriptTests.java index 278acaa6f82..92c1012a523 100644 --- a/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/DefaultLaunchScriptTests.java +++ b/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/DefaultLaunchScriptTests.java @@ -111,6 +111,11 @@ public class DefaultLaunchScriptTests { assertThatPlaceholderCanBeReplaced("confFolder"); } + @Test + public void stopWaitTimeCanBeReplaced() throws Exception { + assertThatPlaceholderCanBeReplaced("stopWaitTime"); + } + @Test public void defaultForUseStartStopDaemonIsTrue() throws Exception { DefaultLaunchScript script = new DefaultLaunchScript(null, null); @@ -125,6 +130,13 @@ public class DefaultLaunchScriptTests { assertThat(content).contains("MODE=\"auto\""); } + @Test + public void defaultForStopWaitTimeIs60() throws Exception { + DefaultLaunchScript script = new DefaultLaunchScript(null, null); + String content = new String(script.toByteArray()); + assertThat(content).contains("STOP_WAIT_TIME=60"); + } + @Test public void loadFromFile() throws Exception { File file = this.temporaryFolder.newFile();