From 348b3b7028b0d8731564d4bda1f074711b9df3be Mon Sep 17 00:00:00 2001 From: lanyuanxiaoyao Date: Mon, 29 Jan 2024 09:09:20 +0800 Subject: [PATCH] =?UTF-8?q?refactor(cli):=20=E4=BC=98=E5=8C=96=E5=81=9C?= =?UTF-8?q?=E6=AD=A2=E8=84=9A=E6=9C=AC=20=E5=A2=9E=E5=8A=A0=E8=B6=85?= =?UTF-8?q?=E6=97=B6=E5=90=8E=E5=BC=BA=E5=88=B6kill=E5=BA=94=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/cli/runner/RunnerApplication.java | 105 +++++++----------- .../main/resources/template/force-stop.ftl | 14 --- 2 files changed, 41 insertions(+), 78 deletions(-) delete mode 100644 service-cli/service-cli-runner/src/main/resources/template/force-stop.ftl diff --git a/service-cli/service-cli-runner/src/main/java/com/lanyuanxiaoyao/service/cli/runner/RunnerApplication.java b/service-cli/service-cli-runner/src/main/java/com/lanyuanxiaoyao/service/cli/runner/RunnerApplication.java index 9d72147..b54e59d 100644 --- a/service-cli/service-cli-runner/src/main/java/com/lanyuanxiaoyao/service/cli/runner/RunnerApplication.java +++ b/service-cli/service-cli-runner/src/main/java/com/lanyuanxiaoyao/service/cli/runner/RunnerApplication.java @@ -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 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()); diff --git a/service-cli/service-cli-runner/src/main/resources/template/force-stop.ftl b/service-cli/service-cli-runner/src/main/resources/template/force-stop.ftl deleted file mode 100644 index 80b8755..0000000 --- a/service-cli/service-cli-runner/src/main/resources/template/force-stop.ftl +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/bash - -hosts=( -<#list hosts as host> - ${host} - -) - -for host in <#noparse>${hosts[@]} -do - hostname=`ssh $host 'echo $HOSTNAME'` - echo "$host $hostname" - ssh $host 'bash -s' < force-stop.sh ${runtime.jarPath}/${info.name}.jar -done