refactor(cli): 优化停止脚本 增加超时后强制kill应用

This commit is contained in:
2024-01-29 09:09:20 +08:00
parent c4260e7bf9
commit 348b3b7028
2 changed files with 41 additions and 78 deletions

View File

@@ -21,7 +21,6 @@ import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -150,25 +149,6 @@ public class RunnerApplication implements ApplicationRunner {
Files.deleteIfExists(stopScriptFile);
Files.write(stopScriptFile, stopScript.getBytes());
Template forceStopTemplate = engine.getTemplate("force-stop.ftl");
String forceStopScript = forceStopTemplate.render(MapUtil.builder()
.put("hosts", deployInformationProperties.getHosts()
.stream()
.map(HostInfo::getIp)
.sorted(Comparator.naturalOrder())
.collect(Collectors.toList()))
.put("runtime", deployInformationProperties.getRuntime())
.put("info", serviceInfo)
.put("arguments", serviceInfo.getArguments())
.put("environments", serviceInfo.getEnvironments())
.build());
Path forceStopScriptFile = Paths.get(
root.toString(),
StrUtil.format("force-stop-{}.sh", serviceInfo.getName())
);
Files.deleteIfExists(forceStopScriptFile);
Files.write(forceStopScriptFile, forceStopScript.getBytes());
Template logTemplate = engine.getTemplate("log.ftl");
String logScript = logTemplate.render(MapUtil.builder()
.put("hosts", deployInformationProperties.getHosts()
@@ -190,50 +170,47 @@ public class RunnerApplication implements ApplicationRunner {
Files.write(logScriptFile, logScript.getBytes());
}
Function<Boolean, String> stopScript = isForce -> "original_command=\"$0\"\n" +
"application_name=\"$1\"\n" +
"function get_pid() {\n" +
" PID=$1\n" +
" if [[ ! -z \"$PID\" ]]; then\n" +
" ID=$(ps -ef | grep \"$application_name\" | grep \"$PID\" | grep -v grep | grep -v $original_command | awk '{ print $2 }')\n" +
" if [[ -z \"$ID\" ]]; then\n" +
" ID=$(ps -C java -f --width 1000 | grep \"$application_name\" | grep \"$PID\" | grep -v grep | grep -v $original_command | awk '{print $2}')\n" +
" if [[ -z \"$ID\" ]]; then\n" +
" ID=$(ps aux | grep \"$application_name\" | grep \"$PID\" | grep -v grep | grep -v $original_command | awk '{print $2}')\n" +
" fi\n" +
" fi\n" +
" else\n" +
" ID=$(ps -ef | grep \"$application_name\" | grep -v grep | grep -v $original_command | awk '{ print $2 }')\n" +
" if [[ -z \"$ID\" ]]; then\n" +
" ID=$(ps -C java -f --width 1000 | grep \"$application_name\" | grep -v grep | grep -v $original_command | awk '{print $2}')\n" +
" if [[ -z \"$ID\" ]]; then\n" +
" ID=$(ps aux | grep \"$application_name\" | grep -v grep | grep -v $original_command | awk '{print $2}')\n" +
" fi\n" +
" fi\n" +
" fi\n" +
" echo ${ID}\n" +
"}\n" +
"\n" +
"pid=$(get_pid)\n" +
"if [[ -z \"$pid\" ]]; then\n" +
" echo \"Application is already stopped.\"\n" +
"else\n" +
" echo \"Stopping $pid...\"\n" +
" kill" + (isForce ? " -9 " : " ") + "${pid}\n" +
"fi\n" +
"\n" +
"LOOPS=0\n" +
"while (true); do\n" +
" gpid=$(get_pid \"$pid\")\n" +
" if [[ \"$gpid\" == \"\" ]]; then\n" +
" echo \"Oook! cost:$LOOPS\"\n" +
" break\n" +
" fi\n" +
" let LOOPS=LOOPS+1\n" +
" sleep 1\n" +
"done\n";
Files.write(Paths.get(root.toString(), "stop.sh"), stopScript.apply(false).getBytes());
Files.write(Paths.get(root.toString(), "force-stop.sh"), stopScript.apply(true).getBytes());
// language=JavaScript
String stopScript =
"original_command=\"$0\"\n" +
"application_name=\"$1\"\n" +
"\n" +
"function get_pid() {\n" +
" ID=$(ps -ef | grep \"$application_name\" | grep -v grep | grep -v $original_command | awk '{ print $2 }')\n" +
" if [[ -z \"$ID\" ]]; then\n" +
" ID=$(ps -C java -f --width 1000 | grep \"$application_name\" | grep -v grep | grep -v $original_command | awk '{print $2}')\n" +
" if [[ -z \"$ID\" ]]; then\n" +
" ID=$(ps aux | grep \"$application_name\" | grep -v grep | grep -v $original_command | awk '{print $2}')\n" +
" fi\n" +
" fi\n" +
" echo ${ID}\n" +
"}\n" +
"\n" +
"pid=$(get_pid)\n" +
"\n" +
"if [[ -z \"$pid\" ]]; then\n" +
" echo \"Application is already stopped.\"\n" +
" exit 0\n" +
"fi\n" +
"\n" +
"echo \"Stopping $pid...\"\n" +
"kill ${pid}\n" +
"\n" +
"LOOPS=0\n" +
"while (true); do\n" +
" cpid=$(get_pid)\n" +
" if [[ -z \"$cpid\" ]]; then\n" +
" echo \"Oook! cost:$LOOPS\"\n" +
" break\n" +
" fi\n" +
" LOOPS=$(($LOOPS+1))\n" +
" if [[ $(($LOOPS%15)) -eq 0 ]]; then\n" +
" echo \"Wait timeout, try to force kill ${pid}...\"\n" +
" kill ${pid}\n" +
" fi\n" +
" sleep 1s\n" +
"done";
Files.write(Paths.get(root.toString(), "stop.sh"), stopScript.getBytes());
Files.deleteIfExists(planPath);
Files.write(planPath, mapper.writeValueAsString(deployPlans).getBytes());

View File

@@ -1,14 +0,0 @@
#!/bin/bash
hosts=(
<#list hosts as host>
${host}
</#list>
)
for host in <#noparse>${hosts[@]}</#noparse>
do
hostname=`ssh $host 'echo $HOSTNAME'`
echo "$host $hostname"
ssh $host 'bash -s' < force-stop.sh ${runtime.jarPath}/${info.name}.jar
done