[server][ executor]- 优化部分代码 - 2

This commit is contained in:
zeaslity
2023-02-28 17:13:15 +08:00
parent 65ed141697
commit 00858e9184
2 changed files with 55 additions and 4 deletions

View File

@@ -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
);

View File

@@ -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<String> 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);
}
});