refactor(cli): 优化停止脚本查询线程的方案

加入使用jps命令查询线程,解决ps匹配不到部分线程的问题
This commit is contained in:
v-zhangjc9
2024-03-21 17:33:24 +08:00
parent bbcb6b3f08
commit 03bb3243bc
7 changed files with 28 additions and 19 deletions

View File

@@ -193,9 +193,14 @@ public class RunnerApplication implements ApplicationRunner {
Files.write(logScriptFile, logScript.getBytes());
}
ClassPathResource stopFile = new ClassPathResource("script/stop.sh");
String stopScript = new String(IoUtil.readBytes(stopFile.getInputStream()));
Files.write(Paths.get(root.toString(), "stop.sh"), stopScript.getBytes());
Template stopTemplate = engine.getTemplate("cloud/stop-script.ftl");
String stopScript = stopTemplate.render(MapUtil.builder()
.put("currentPath", absolutRootPath)
.put("runtime", runtimeInfo)
.build());
Path stopScriptFile = Paths.get(root.toString(), "stop.sh");
Files.deleteIfExists(stopScriptFile);
Files.write(stopScriptFile, stopScript.getBytes());
MutableMap<String, MutableList<ServiceInfoWrapper>> groups = Maps.mutable.empty();
for (ServiceInfoWrapper service : serviceInfoList) {
@@ -278,6 +283,7 @@ public class RunnerApplication implements ApplicationRunner {
}
private void generateUploader(Path root) throws IOException {
Files.createDirectories(root);
String absolutRootPath = root.toAbsolutePath().toString();
logger.info("Current path: {}", absolutRootPath);
@@ -300,10 +306,13 @@ public class RunnerApplication implements ApplicationRunner {
Files.deleteIfExists(updateScriptFile);
Files.write(updateScriptFile, updateScript.getBytes());
ClassPathResource stopFile = new ClassPathResource("script/stop.sh");
String stopScript = new String(IoUtil.readBytes(stopFile.getInputStream()));
Template stopTemplate = engine.getTemplate("cloud/stop-script.ftl");
String stopScript = stopTemplate.render(MapUtil.builder()
.put("currentPath", absolutRootPath)
.put("runtime", runtimeInfo)
.build());
// 一个魔法操作
stopScript = stopScript.replaceAll("\\$1", "service-uploader.jar");
stopScript = stopScript.replaceAll("application_name=\".*\"", "application_name=\"service-uploader.jar\"");
Files.write(Paths.get(root.toString(), "stop.sh"), stopScript.getBytes());
}

View File

@@ -6,8 +6,8 @@ deploy:
user: datalake
# 应用jar包
jar-path: /data/datalake/jars
# 主机jdk
jdk-path: /opt/jdk8u252-b09/bin/java
# 主机jdk根目录
jdk-path: /opt/jdk8u252-b09
# 应用日志
log-path: /data/datalake/logs
# 应用配置

View File

@@ -3,7 +3,7 @@ deploy:
signature: b5
user: datalake
jar-path: /apps/datalake/hudi/jars
jdk-path: /opt/jdk1.8.0_162/bin/java
jdk-path: /opt/jdk1.8.0_162
log-path: /apps/datalake/hudi/logs
conf-path: /apps/datalake/hudi/conf
data-path: /apps/datalake/hudi/data

View File

@@ -13,7 +13,7 @@ hostname_full=`ssh $host 'hostname -f'`
<#-- 获取当前时间 -->
start_time=`date +%Y%m%d%H%M%S`
ssh $host "mkdir -p ${runtime.jarPath};curl ${runtime.downloadUrl}/${info.sourceJar} -o ${runtime.jarPath}/${info.name}.jar"
ssh $host "export JASYPT_ENCRYPTOR_PASSWORD='r#(R,P\"Dp^A47>WSn:Wn].gs/+\"v:q_Q*An~zF*g-@j@jtSTv5H/,S-3:R?r9R}.';nohup ${runtime.jdkPath} <#list environments?keys as key>-D${key}=${environments[key]?string} </#list>-jar ${runtime.jarPath}/${info.name}.jar <#noparse>--deploy.datetime=${datetime} --deploy.ip=${host} --deploy.hostname=${hostname} --deploy.hostname-full=${hostname_full} --deploy.start-time=${start_time}</#noparse> --hudi-service.signature=${runtime.signature} --logging.parent=${runtime.logPath} --loki.url=${runtime.loki.servicePushUrl} --spring.cloud.zookeeper.connect-string=${runtime.zkUrl} --spring.security.meta.authority='${runtime.security.authority}' --spring.security.meta.username='${runtime.security.username}' --spring.security.meta.darkcode='${runtime.security.darkcode}' --yarn-cluster.sync-clusters=${runtime.yarn.syncClusters} --yarn-cluster.compaction-clusters=${runtime.yarn.compactionClusters} <#list arguments?keys as key>--${key}=${arguments[key]?string} </#list>> /dev/null 2>&1 &"
ssh $host "export JASYPT_ENCRYPTOR_PASSWORD='r#(R,P\"Dp^A47>WSn:Wn].gs/+\"v:q_Q*An~zF*g-@j@jtSTv5H/,S-3:R?r9R}.';nohup ${runtime.jdkPath}/bin/java <#list environments?keys as key>-D${key}=${environments[key]?string} </#list>-jar ${runtime.jarPath}/${info.name}.jar <#noparse>--deploy.datetime=${datetime} --deploy.ip=${host} --deploy.hostname=${hostname} --deploy.hostname-full=${hostname_full} --deploy.start-time=${start_time}</#noparse> --hudi-service.signature=${runtime.signature} --logging.parent=${runtime.logPath} --loki.url=${runtime.loki.servicePushUrl} --spring.cloud.zookeeper.connect-string=${runtime.zkUrl} --spring.security.meta.authority='${runtime.security.authority}' --spring.security.meta.username='${runtime.security.username}' --spring.security.meta.darkcode='${runtime.security.darkcode}' --yarn-cluster.sync-clusters=${runtime.yarn.syncClusters} --yarn-cluster.compaction-clusters=${runtime.yarn.compactionClusters} <#list arguments?keys as key>--${key}=${arguments[key]?string} </#list>> /dev/null 2>&1 &"
</#if>
echo ''
</#list>

View File

@@ -4,14 +4,14 @@ original_command="$0"
application_name="$1"
function get_pid() {
ID=$(ps -ef | grep "$application_name" | grep -v grep | grep -v $original_command | awk '{ print $2 }')
ID=$(ps ef | grep "$application_name" | grep -v grep | grep -v $original_command | awk '{ print $2 }')
if [[ -z "$ID" ]]; then
ID=$(ps -C java -f --width 1000 | grep "$application_name" | grep -v grep | grep -v $original_command | awk '{print $2}')
ID=$(ps aux | grep "$application_name" | grep -v grep | grep -v $original_command | awk '{print $2}')
if [[ -z "$ID" ]]; then
ID=$(ps aux | grep "$application_name" | grep -v grep | grep -v $original_command | awk '{print $2}')
ID=$(${runtime.jdkPath}/bin/jps -lmvV | grep "$application_name" | awk '{print $1}')
fi
fi
echo ${ID}
echo $ID
}
pid=$(get_pid)
@@ -22,7 +22,7 @@ if [[ -z "$pid" ]]; then
fi
echo "Stopping $pid..."
kill ${pid}
kill $pid
LOOPS=0
while (true); do
@@ -33,8 +33,8 @@ while (true); do
fi
LOOPS=$(($LOOPS+1))
if [[ $(($LOOPS%15)) -eq 0 ]]; then
echo "Wait timeout, try to force kill ${pid}..."
kill -9 ${pid}
echo "Wait timeout, try to force kill $pid..."
kill -9 $pid
fi
sleep 1s
done

View File

@@ -1,4 +1,4 @@
#!/bin/bash
mkdir -p ${runtime.jarPath}
export JASYPT_ENCRYPTOR_PASSWORD='r#(R,P\"Dp^A47>WSn:Wn].gs/+\"v:q_Q*An~zF*g-@j@jtSTv5H/,S-3:R?r9R}.'
${runtime.jdkPath} <#noparse>-Ddeploy.datetime=$(date +%Y%m%d%H%M%S) -Ddeploy.hostname=$(hostname) -Ddeploy.hostname-full=$(hostname -f) -Ddeploy.start-time=$(date +%Y%m%d%H%M%S)</#noparse> -Dlogging.parent=${runtime.logPath} -Dloki.url=${runtime.loki.servicePushUrl} -Dspring.cloud.zookeeper.connect-string=${runtime.zkUrl} -Dyarn-cluster.sync-clusters=${runtime.yarn.syncClusters} -Dyarn-cluster.compaction-clusters=${runtime.yarn.compactionClusters} -jar ${runtime.jarPath}/service-command.jar<#if directly> $@</#if>
${runtime.jdkPath}/bin/java <#noparse>-Ddeploy.datetime=$(date +%Y%m%d%H%M%S) -Ddeploy.hostname=$(hostname) -Ddeploy.hostname-full=$(hostname -f) -Ddeploy.start-time=$(date +%Y%m%d%H%M%S)</#noparse> -Dlogging.parent=${runtime.logPath} -Dloki.url=${runtime.loki.servicePushUrl} -Dspring.cloud.zookeeper.connect-string=${runtime.zkUrl} -Dyarn-cluster.sync-clusters=${runtime.yarn.syncClusters} -Dyarn-cluster.compaction-clusters=${runtime.yarn.compactionClusters} -jar ${runtime.jarPath}/service-command.jar<#if directly> $@</#if>

View File

@@ -1,4 +1,4 @@
#!/bin/bash
mkdir -p ${runtime.jarPath}
export JASYPT_ENCRYPTOR_PASSWORD='r#(R,P"Dp^A47>WSn:Wn].gs/+"v:q_Q*An~zF*g-@j@jtSTv5H/,S-3:R?r9R}.'
nohup ${runtime.jdkPath} -jar ${runtime.jarPath}/service-uploader.jar <#noparse>--deploy.datetime=$(date +%Y%m%d%H%M%S) --deploy.hostname=$(hostname) --deploy.hostname-full=$(hostname -f) --deploy.start-time=$(date +%Y%m%d%H%M%S)</#noparse> --logging.parent=${runtime.logPath} --loki.url=${runtime.loki.servicePushUrl} --spring.cloud.zookeeper.connect-string=${runtime.zkUrl} --uploader.backend=local --uploader.tmp-dir=${runtime.dataPath}/uploader --spring.security.meta.authority='${runtime.security.authority}' --spring.security.meta.username='${runtime.security.username}' --spring.security.meta.darkcode='${runtime.security.darkcode}' > /dev/null 2>&1 &
nohup ${runtime.jdkPath}/bin/java -jar ${runtime.jarPath}/service-uploader.jar <#noparse>--deploy.datetime=$(date +%Y%m%d%H%M%S) --deploy.hostname=$(hostname) --deploy.hostname-full=$(hostname -f) --deploy.start-time=$(date +%Y%m%d%H%M%S)</#noparse> --logging.parent=${runtime.logPath} --loki.url=${runtime.loki.servicePushUrl} --spring.cloud.zookeeper.connect-string=${runtime.zkUrl} --uploader.backend=local --uploader.tmp-dir=${runtime.dataPath}/uploader --spring.security.meta.authority='${runtime.security.authority}' --spring.security.meta.username='${runtime.security.username}' --spring.security.meta.darkcode='${runtime.security.darkcode}' > /dev/null 2>&1 &