From 00858e9184ebc12f1717668ddb5a356a8e945a54 Mon Sep 17 00:00:00 2001 From: zeaslity Date: Tue, 28 Feb 2023 17:13:15 +0800 Subject: [PATCH] =?UTF-8?q?[server][=20executor]-=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E9=83=A8=E5=88=86=E4=BB=A3=E7=A0=81=20-=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wdd/agent/executor/CommandExecutor.java | 4 +- .../executor/thread/CommandExecLogCache.java | 55 ++++++++++++++++++- 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/agent/src/main/java/io/wdd/agent/executor/CommandExecutor.java b/agent/src/main/java/io/wdd/agent/executor/CommandExecutor.java index b805cb4..e2540a9 100644 --- a/agent/src/main/java/io/wdd/agent/executor/CommandExecutor.java +++ b/agent/src/main/java/io/wdd/agent/executor/CommandExecutor.java @@ -182,8 +182,6 @@ public class CommandExecutor { throw new RuntimeException(e); } finally { - System.out.println("process isAlive = " + process.isAlive()); - // 任务提前执行结束,或者超过了最长等待时间 // 判断命令是否正确处理完成 if (!commandExecComplete) { @@ -212,7 +210,7 @@ public class CommandExecutor { //commandExecLogCache.PrintCommandCachedLog(streamKey); // 关停任务执行的缓存日志收集 BufferedReader 否则无法终止 - commandExecLogCache.StopExecLogBufferedReader( + commandExecLogCache.StopCollectExecLog( streamKey, process ); diff --git a/agent/src/main/java/io/wdd/agent/executor/thread/CommandExecLogCache.java b/agent/src/main/java/io/wdd/agent/executor/thread/CommandExecLogCache.java index 2499504..2ca5aee 100644 --- a/agent/src/main/java/io/wdd/agent/executor/thread/CommandExecLogCache.java +++ b/agent/src/main/java/io/wdd/agent/executor/thread/CommandExecLogCache.java @@ -161,6 +161,13 @@ public class CommandExecLogCache { commandCachedLog::add ); + // 对于即时执行完成的任务,需要在这里增加尾巴内容 + addCommandExecTailInfo( + process, + streamKey, + false + ); + log.debug( "命令代码 [ {} ] 的执行日志内容为 {} ", streamKey, @@ -170,11 +177,46 @@ public class CommandExecLogCache { ); } + /** + * 增加尾巴内容 + * + * @param process + * @param streamKey + */ + private void addCommandExecTailInfo(Process process, String streamKey, boolean isKillProcess) { + + log.debug("开始添加任务日志的尾部信息!"); + + // 添加任务结束的一些信息 + String execTimeCostString = String.format( + "execution time-cost is => [ %s ]", + process + .info() + .totalCpuDuration() + ); + + // 是否需要强行关闭 process 杀死任务进程 + if (isKillProcess) { + process.destroyForcibly(); + } + + String execResultString = String.format( + "execution result code is => [ %s ]", + process.exitValue() + ); + + ArrayList commandExecCachedLog = CachedCommandLogMap.get(streamKey); + + commandExecCachedLog.add("--------------- command result are as above --------------------"); + commandExecCachedLog.add(execTimeCostString); + commandExecCachedLog.add(execResultString); + } + /** * 对于一些没有中止的任务,必须要手动将读取的 InputStream流关闭 * 否则部分任务的日志无法收集 */ - public void StopExecLogBufferedReader(String streamKey, Process process) { + public void StopCollectExecLog(String streamKey, Process process) { BufferedReader bufferedReader = CommandLogBufferedReaderMap.get(streamKey); @@ -202,6 +244,17 @@ public class CommandExecLogCache { streamKey ); + // 任务卡住了到现在,需要强行中止,并且添加部分日志信息 + if (ObjectUtils.isNotEmpty(process) && process.isAlive()) { + + addCommandExecTailInfo( + process, + streamKey, + true + ); + } + + // 移除 CommandLogBufferedReaderMap.remove(streamKey); } });