[server][status]- 优化command executor的代码
This commit is contained in:
@@ -11,7 +11,6 @@ import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
@@ -76,15 +75,23 @@ public class CommandExecutor {
|
||||
|
||||
|
||||
private int processExecute(String streamKey, ProcessBuilder processBuilder) {
|
||||
|
||||
// 重定向,错误日志到标准输出中
|
||||
processBuilder.redirectErrorStream(true);
|
||||
// processBuilder.inheritIO();
|
||||
processBuilder.directory(new File(System.getProperty("user.home")));
|
||||
//processBuilder.inheritIO();
|
||||
//processBuilder.directory(new File(System.getProperty("user.home")));
|
||||
int processResult = 233;
|
||||
|
||||
try {
|
||||
|
||||
// 开始执行命令之前,需要进行打印
|
||||
log.debug(
|
||||
"current shell command {}",
|
||||
processBuilder.command()
|
||||
);
|
||||
|
||||
// 开始执行命令操作
|
||||
Process process = processBuilder.start();
|
||||
|
||||
// start a backend thread to daemon the process
|
||||
// wait for processMaxWaitSeconds and kill the process if it's still alived
|
||||
DaemonCommandProcess.submit(
|
||||
@@ -93,14 +100,15 @@ public class CommandExecutor {
|
||||
processMaxWaitSeconds
|
||||
));
|
||||
|
||||
// cache log lines
|
||||
|
||||
// 缓存让命令处理日志,并且打印
|
||||
logToArrayListCache.cacheLog(
|
||||
streamKey,
|
||||
process
|
||||
);
|
||||
|
||||
// start to send the result log
|
||||
streamSender.startToWaitLog(streamKey);
|
||||
//streamSender.startToWaitLog(streamKey);
|
||||
|
||||
// todo this will stuck the process and rabbitmq message will reentry the queue
|
||||
// get the command result must also be a timeout smaller than the process
|
||||
@@ -109,8 +117,10 @@ public class CommandExecutor {
|
||||
TimeUnit.SECONDS
|
||||
);
|
||||
|
||||
|
||||
|
||||
// end send logs
|
||||
streamSender.endWaitLog(streamKey);
|
||||
//streamSender.endWaitLog(streamKey);
|
||||
|
||||
// get the process result
|
||||
if (ObjectUtils.isNotEmpty(waitFor) && ObjectUtils.isNotEmpty(process)) {
|
||||
|
||||
@@ -14,7 +14,6 @@ import java.util.List;
|
||||
|
||||
/**
|
||||
* utils to cache store the command execution logs
|
||||
*
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
@@ -35,7 +34,13 @@ public class LogToArrayListCache {
|
||||
|
||||
ArrayList<String> commandCachedLog = this.getExecutionCmdCachedLogArrayList(streamKey);
|
||||
|
||||
String format = String.format("execution command are => [ %s ]", process.info().commandLine().get());
|
||||
String format = String.format(
|
||||
"execution command are => [ %s ]",
|
||||
process
|
||||
.info()
|
||||
.commandLine()
|
||||
.get()
|
||||
);
|
||||
|
||||
// add the command
|
||||
commandCachedLog.add("");
|
||||
@@ -44,16 +49,17 @@ public class LogToArrayListCache {
|
||||
commandCachedLog.add("");
|
||||
|
||||
// cache the real command logs
|
||||
cacheLog(streamKey, process.getInputStream());
|
||||
doCacheLog(
|
||||
streamKey,
|
||||
process.getInputStream()
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
public void cacheLog(String streamKey, InputStream commandLogStream) {
|
||||
private void doCacheLog(String streamKey, InputStream commandLogStream) {
|
||||
|
||||
ArrayList<String> commandCachedLog = this.getExecutionCmdCachedLogArrayList(streamKey);
|
||||
|
||||
// log.info(String.valueOf(commandCachedLog));
|
||||
|
||||
// read from input stream and store to the cacheArrayList
|
||||
new BufferedReader(new InputStreamReader(commandLogStream))
|
||||
.lines()
|
||||
@@ -61,7 +67,11 @@ public class LogToArrayListCache {
|
||||
commandCachedLog::add
|
||||
);
|
||||
|
||||
log.debug("current streamKey is {} and CacheLog is {}", streamKey, commandCachedLog);
|
||||
log.debug(
|
||||
"current streamKey is {} and CacheLog is {}",
|
||||
streamKey,
|
||||
commandCachedLog
|
||||
);
|
||||
}
|
||||
|
||||
public ArrayList<String> getExecutionCmdCachedLogArrayList(String streamKey) {
|
||||
@@ -85,9 +95,9 @@ public class LogToArrayListCache {
|
||||
boolean hasRehashed = false;
|
||||
|
||||
if (CollectionUtils.isNotEmpty(CachedCommandLog.get(result))) {
|
||||
for (int index = result+1; index < CachedCommandLog.size(); index++) {
|
||||
for (int index = result + 1; index < CachedCommandLog.size(); index++) {
|
||||
// from the index to the end
|
||||
if (CollectionUtils.isEmpty(CachedCommandLog.get(index))){
|
||||
if (CollectionUtils.isEmpty(CachedCommandLog.get(index))) {
|
||||
hasRehashed = true;
|
||||
result = index;
|
||||
break;
|
||||
@@ -96,7 +106,7 @@ public class LogToArrayListCache {
|
||||
if (!hasRehashed) {
|
||||
for (int index = 0; index < result; index++) {
|
||||
// from begin to the index
|
||||
if (CollectionUtils.isEmpty(CachedCommandLog.get(index))){
|
||||
if (CollectionUtils.isEmpty(CachedCommandLog.get(index))) {
|
||||
hasRehashed = true;
|
||||
result = index;
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user