[agent][executor]- bug - 5

This commit is contained in:
zeaslity
2023-02-25 22:37:27 +08:00
parent c1c698adbb
commit db8c4a216e

View File

@@ -1,6 +1,7 @@
package io.wdd.agent.executor.thread; package io.wdd.agent.executor.thread;
import io.wdd.agent.config.utils.AgentCommonThreadPool;
import io.wdd.common.handler.MyRuntimeException; import io.wdd.common.handler.MyRuntimeException;
import io.wdd.common.utils.TimeUtils; import io.wdd.common.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@@ -8,7 +9,6 @@ import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@@ -38,48 +38,24 @@ public class CommandExecLogCache {
*/ */
public void cacheLog(String streamKey, Process process) { public void cacheLog(String streamKey, Process process) {
log.debug( ProcessHandle.Info info = process.info();
"开始缓存命令执行日志! process is {}",
process.info()
);
if (ObjectUtils.isEmpty(process.info())) { if (ObjectUtils.isEmpty(info)) {
log.error("process is null ! cache log error !"); log.error("process is null ! cache log error !");
throw new MyRuntimeException(); throw new MyRuntimeException();
} }
ArrayList<String> commandCachedLog = new ArrayList<>(128);
String execCommandString = String.format(
"execution command are => [ %s ]",
process
.info()
.commandLine()
.get()
);
String execTimeString = String.format(
"execution time is => [ %s ]",
TimeUtils.currentTimeString()
);
// add the command
commandCachedLog.add("");
commandCachedLog.add(execCommandString);
commandCachedLog.add(execTimeString);
commandCachedLog.add("--------------- command result are as below --------------------");
commandCachedLog.add("");
log.debug( log.debug(
"命令执行结果头信息操作完成,内容为 {}", "开始缓存命令执行日志! process is {}",
commandCachedLog info
); );
// cache the real command logs // 这里需要采用异步的方式执行
doCacheLog( AgentCommonThreadPool.pool.submit(
streamKey, () -> // cache the real command logs
process.getInputStream(), doCacheLog(
commandCachedLog streamKey,
process
)
); );
} }
@@ -145,15 +121,41 @@ public class CommandExecLogCache {
* 实际执行命令缓存操作 * 实际执行命令缓存操作
* *
* @param streamKey * @param streamKey
* @param commandLogStream
* @param commandCachedLog
*/ */
private void doCacheLog(String streamKey, InputStream commandLogStream, ArrayList<String> commandCachedLog) { private void doCacheLog(String streamKey, Process process) {
log.debug("开始从process的结果中获取日志缓存"); ArrayList<String> commandCachedLog = new ArrayList<>(128);
String execCommandString = String.format(
"execution command are => [ %s ]",
process
.info()
.commandLine()
.get()
);
String execTimeString = String.format(
"execution time is => [ %s ]",
TimeUtils.currentTimeString()
);
// add the command
commandCachedLog.add("");
commandCachedLog.add(execCommandString);
commandCachedLog.add(execTimeString);
commandCachedLog.add("--------------- command result are as below --------------------");
commandCachedLog.add("");
log.debug(
"命令执行结果头信息操作完成,内容为 {}",
commandCachedLog
);
log.debug("doCacheLog 开始从process的结果中获取日志缓存");
// read from input stream and store to the cacheArrayList // read from input stream and store to the cacheArrayList
new BufferedReader(new InputStreamReader(commandLogStream)) new BufferedReader(new InputStreamReader(process.getInputStream()))
.lines() .lines()
.forEach( .forEach(
commandCachedLog::add commandCachedLog::add