From db8c4a216ebbf92705b65c405211d8df1deb24f8 Mon Sep 17 00:00:00 2001 From: zeaslity Date: Sat, 25 Feb 2023 22:37:27 +0800 Subject: [PATCH] [agent][executor]- bug - 5 --- .../executor/thread/CommandExecLogCache.java | 84 ++++++++++--------- 1 file changed, 43 insertions(+), 41 deletions(-) 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 b6dbcb7..e26acc5 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 @@ -1,6 +1,7 @@ package io.wdd.agent.executor.thread; +import io.wdd.agent.config.utils.AgentCommonThreadPool; import io.wdd.common.handler.MyRuntimeException; import io.wdd.common.utils.TimeUtils; import lombok.extern.slf4j.Slf4j; @@ -8,7 +9,6 @@ import org.apache.commons.lang3.ObjectUtils; import org.springframework.stereotype.Service; import java.io.BufferedReader; -import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; @@ -38,48 +38,24 @@ public class CommandExecLogCache { */ public void cacheLog(String streamKey, Process process) { - log.debug( - "开始缓存命令执行日志! process is {}", - process.info() - ); + ProcessHandle.Info info = process.info(); - if (ObjectUtils.isEmpty(process.info())) { + if (ObjectUtils.isEmpty(info)) { log.error("process is null ! cache log error !"); throw new MyRuntimeException(); } - - ArrayList 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 + "开始缓存命令执行日志! process is {}", + info ); - // cache the real command logs - doCacheLog( - streamKey, - process.getInputStream(), - commandCachedLog + // 这里需要采用异步的方式执行 + AgentCommonThreadPool.pool.submit( + () -> // cache the real command logs + doCacheLog( + streamKey, + process + ) ); } @@ -145,15 +121,41 @@ public class CommandExecLogCache { * 实际执行命令缓存操作 * * @param streamKey - * @param commandLogStream - * @param commandCachedLog */ - private void doCacheLog(String streamKey, InputStream commandLogStream, ArrayList commandCachedLog) { + private void doCacheLog(String streamKey, Process process) { - log.debug("开始从process的结果中获取日志缓存"); + ArrayList 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 - new BufferedReader(new InputStreamReader(commandLogStream)) + new BufferedReader(new InputStreamReader(process.getInputStream())) .lines() .forEach( commandCachedLog::add