[server][status]- 优化command executor的代码

This commit is contained in:
zeaslity
2023-02-22 10:17:20 +08:00
parent 86b59181e5
commit 24033bacc1
5 changed files with 95 additions and 25 deletions

36
.run/Agent-dev-1.run.xml Normal file
View File

@@ -0,0 +1,36 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Agent-dev-1" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot" activateToolWindowBeforeRun="false">
<option name="ALTERNATIVE_JRE_PATH" value="11" />
<envs>
<env name="agentVersion" value="2023-02-13" />
<env name="archInfo" value="&quot;x86_64 (64 Bit)&quot;" />
<env name="cpuBrand" value="&quot;Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz&quot;" />
<env name="cpuCore" value="&quot;12 @ 4299.998 MHz&quot;" />
<env name="diskTotal" value="&quot;914.9 GB&quot;" />
<env name="diskUsage" value="&quot;12.3 GB&quot;" />
<env name="ioSpeed" value="&quot;259 MB/s&quot;" />
<env name="location" value="&quot;Chengdu Sichuan CN&quot;" />
<env name="machineId" value="&quot;remote-dev-19&quot;" />
<env name="managePort" value="&quot;22&quot;" />
<env name="memoryTotal" value="&quot;7.6 GB&quot;" />
<env name="osInfo" value="&quot;Ubuntu 20.04.5 LTS&quot;" />
<env name="osKernelInfo" value="&quot;5.4.0-135-generic&quot;" />
<env name="provider" value="&quot;AS139080 The Internet Data Center of Sichuan Mobile Communication Company Limited&quot;" />
<env name="serverIpInV4" value="&quot;&quot;" />
<env name="serverIpInV6" value="&quot;&quot;" />
<env name="serverIpPbV4" value="&quot;183.220.149.17&quot;" />
<env name="serverIpPbV6" value="&quot;&quot;" />
<env name="serverName" value="&quot;Chengdu-amd64-77&quot;" />
<env name="tcpControl" value="&quot;cubic&quot;" />
<env name="virtualization" value="&quot;Dedicated&quot;" />
<env name="server.port" value="8001" />
</envs>
<module name="agent" />
<target name="root@10.250.0.18:22" />
<option name="SPRING_BOOT_MAIN_CLASS" value="io.wdd.agent.AgentApplication" />
<option name="VM_PARAMETERS" value="-Dserver.port=8001 -Dfile.encoding=utf-8 -Ddebug=true -Dlogging.level.io.wdd.agent=debug -Dspring.profiles.active=dev -Dspring.cloud.nacos.config.group=dev -Dspring.cloud.nacos.config.extension-configs[0].dataId=common-dev.yaml -Dspring.cloud.nacos.config.extension-configs[0].group=dev" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
</component>

View File

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

View File

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

View File

@@ -14,7 +14,9 @@ public class ProxyNodeSet {
public static ProxyNode phoenix2;
public static ProxyNode london2;
// 开发使用
public static ProxyNode chengduAgent;
public static ProxyNode tokyoDev;
static {
@@ -92,7 +94,7 @@ public class ProxyNodeSet {
.agentName("London-amd64-02")
.build();
chengduAgent = ProxyNode
tokyoDev = ProxyNode
.builder()
.location("Tokyo")
.num(99)
@@ -103,6 +105,18 @@ public class ProxyNodeSet {
.agentTopicName("Tokyo-amd64-07-f66a41")
.build();
chengduAgent = ProxyNode
.builder()
.location("Chengdu")
.num(90)
.publicIPv4("183.220.9.13")
.proxyNodeType(ProxyNodeType.EXTERNAL)
.name("chengdu-agent")
.agentName("Chengdu-amd64-77")
.agentTopicName("Chengdu-amd64-77-remote")
.build();
ProxyNodeMap.put(chengdu.getNum(), chengdu);
ProxyNodeMap.put(hongkong.getNum(), hongkong);
ProxyNodeMap.put(shanghai.getNum(), shanghai);
@@ -111,6 +125,7 @@ public class ProxyNodeSet {
ProxyNodeMap.put(phoenix2.getNum(), phoenix2);
ProxyNodeMap.put(london2.getNum(), london2);
ProxyNodeMap.put(chengduAgent.getNum(), chengduAgent);
ProxyNodeMap.put(tokyoDev.getNum(), tokyoDev);
}

View File

@@ -41,7 +41,7 @@ public class XrayConfigDistribute {
static {
ArrayList<String> zero = new ArrayList<>(
/*ArrayList<String> zero = new ArrayList<>(
List.of(
"if",
"[",
@@ -56,7 +56,7 @@ public class XrayConfigDistribute {
";",
"fi"
)
);
);*/
ArrayList<String> first = new ArrayList<>(
@@ -92,13 +92,12 @@ public class XrayConfigDistribute {
"|",
"grep",
"-c",
"active,(running)"
"active (running)"
)
);
updateXrayCommandList.add(zero);
updateXrayCommandList.add(first);
updateXrayCommandList.add(second);
updateXrayCommandList.add(third);
@@ -166,10 +165,10 @@ public class XrayConfigDistribute {
}
// 修改命令中的时间
String s = updateXrayCommandList
.get(1)
.get(0)
.get(2);
updateXrayCommandList
.get(1)
.get(0)
.set(
2,
s.replace(
@@ -180,7 +179,7 @@ public class XrayConfigDistribute {
// 修改命令中的下载url
updateXrayCommandList
.get(2)
.get(1)
.set(
1,
realUrl