[ server ] [ scheduler ]- script scheduler - 1
This commit is contained in:
@@ -10,7 +10,6 @@ import io.wdd.common.beans.rabbitmq.OctopusMessageType;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static io.wdd.agent.config.utils.NacosConfigurationCollector.ALL_FUNCTION_MAP;
|
||||
@@ -31,19 +30,24 @@ public class OMHandlerExecutor extends AbstractOctopusMessageHandler {
|
||||
@Override
|
||||
public boolean handle(OctopusMessage octopusMessage) {
|
||||
|
||||
if (!octopusMessage.getType().equals(OctopusMessageType.EXECUTOR)) {
|
||||
if (!octopusMessage
|
||||
.getType()
|
||||
.equals(OctopusMessageType.EXECUTOR)) {
|
||||
return next.handle(octopusMessage);
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
ExecutionMessage executionMessage = objectMapper.readValue((String) octopusMessage.getContent(), new TypeReference<ExecutionMessage>() {
|
||||
});
|
||||
|
||||
// System.out.println("executionMessage = " + executionMessage);
|
||||
// 需要首先解析成 ExecutionMessage
|
||||
ExecutionMessage executionMessage = objectMapper.readValue(
|
||||
(String) octopusMessage.getContent(),
|
||||
new TypeReference<ExecutionMessage>() {
|
||||
}
|
||||
);
|
||||
|
||||
String executionType = executionMessage.getType();
|
||||
|
||||
|
||||
if (ALL_FUNCTION_MAP.containsKey(executionType)) {
|
||||
// execute the exist function
|
||||
functionExecutor.execute(executionMessage);
|
||||
|
||||
@@ -5,7 +5,7 @@ import com.alibaba.nacos.api.NacosFactory;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import com.alibaba.nacos.api.config.listener.Listener;
|
||||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
import io.wdd.agent.executor.config.FunctionReader;
|
||||
import io.wdd.common.utils.FunctionReader;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
@@ -13,7 +13,6 @@ import org.springframework.stereotype.Component;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
@@ -53,9 +52,6 @@ public class NacosConfigurationCollector {
|
||||
@Value("${octopus.status.name}")
|
||||
public String appStatusDataId;
|
||||
|
||||
@Resource
|
||||
FunctionReader functionReader;
|
||||
|
||||
@PostConstruct
|
||||
private void CollectAllFunctionFromNacos() {
|
||||
|
||||
@@ -158,18 +154,19 @@ public class NacosConfigurationCollector {
|
||||
|
||||
yaml.loadAll(allApplicationNeedToMonitorStatus).iterator().forEachRemaining(realFunction -> {
|
||||
|
||||
// 其实没有意义
|
||||
if (!(realFunction instanceof LinkedHashMap)) {
|
||||
System.out.println("realFunction = " + realFunction);
|
||||
}
|
||||
|
||||
|
||||
Map<String, String> stringMap = (Map<String, String>) realFunction;
|
||||
|
||||
// 拿到实际的功能名称 如 AgentReboot AgentUpdate
|
||||
Optional<String> functionName = stringMap.keySet().stream().findFirst();
|
||||
|
||||
List<List<String>> commandList = functionReader.ReadStringToCommandList(stringMap.get(functionName.get()));
|
||||
List<List<String>> commandList = FunctionReader.ReadStringToCommandList(stringMap.get(functionName.get()));
|
||||
|
||||
/*log.info("Function {} , content is {}", functionName.get(), commandList);*/
|
||||
|
||||
ALL_FUNCTION_MAP.put(functionName.get(), commandList);
|
||||
|
||||
});
|
||||
|
||||
@@ -15,8 +15,9 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
|
||||
@Configuration
|
||||
@@ -39,10 +40,10 @@ public class CommandExecutor {
|
||||
* @param executionMessage get from EXECUTOR_HANDLERju
|
||||
*/
|
||||
public void execute(ExecutionMessage executionMessage) {
|
||||
|
||||
|
||||
this.execute(executionMessage.getResultKey(), executionMessage.getCommandList());
|
||||
|
||||
this.execute(
|
||||
executionMessage.getResultKey(),
|
||||
executionMessage.getCommandList()
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
@@ -51,14 +52,20 @@ public class CommandExecutor {
|
||||
|
||||
ProcessBuilder processBuilder = new ProcessBuilder(command);
|
||||
|
||||
return this.processExecute(streamKey, processBuilder);
|
||||
return this.processExecute(
|
||||
streamKey,
|
||||
processBuilder
|
||||
);
|
||||
}
|
||||
|
||||
public int execute(String streamKey, String... command) {
|
||||
|
||||
ProcessBuilder processBuilder = new ProcessBuilder(command);
|
||||
|
||||
return this.processExecute(streamKey, processBuilder);
|
||||
return this.processExecute(
|
||||
streamKey,
|
||||
processBuilder
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
@@ -82,14 +89,20 @@ public class CommandExecutor {
|
||||
));
|
||||
|
||||
// cache log lines
|
||||
logToArrayListCache.cacheLog(streamKey, process);
|
||||
logToArrayListCache.cacheLog(
|
||||
streamKey,
|
||||
process
|
||||
);
|
||||
|
||||
// start to send the result log
|
||||
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
|
||||
boolean waitFor = process.waitFor(50, TimeUnit.SECONDS);
|
||||
boolean waitFor = process.waitFor(
|
||||
50,
|
||||
TimeUnit.SECONDS
|
||||
);
|
||||
|
||||
// end send logs
|
||||
streamSender.endWaitLog(streamKey);
|
||||
@@ -99,27 +112,45 @@ public class CommandExecutor {
|
||||
processResult = process.exitValue();
|
||||
}
|
||||
|
||||
log.debug("current shell command {} result is {}", processBuilder.command(), processResult);
|
||||
log.debug(
|
||||
"current shell command {} result is {}",
|
||||
processBuilder.command(),
|
||||
processResult
|
||||
);
|
||||
|
||||
|
||||
} catch (IOException | InterruptedException e) {
|
||||
log.error("Shell command error ! {} + {}", e.getCause(), e.getMessage());
|
||||
log.error(
|
||||
"Shell command error ! {} + {}",
|
||||
e.getCause(),
|
||||
e.getMessage()
|
||||
);
|
||||
}
|
||||
|
||||
return processResult;
|
||||
}
|
||||
|
||||
private Runnable StopStuckCommandProcess(Process process, int processMaxWaitSeconds) {
|
||||
private Runnable StopStuckCommandProcess(Process process, int processMaxWaitSeconds) {
|
||||
return () -> {
|
||||
try {
|
||||
|
||||
log.debug("daemon thread start to wait for {} s for the result", processMaxWaitSeconds);
|
||||
log.debug(
|
||||
"daemon thread start to wait for {} s for the result",
|
||||
processMaxWaitSeconds
|
||||
);
|
||||
|
||||
TimeUnit.SECONDS.sleep(processMaxWaitSeconds);
|
||||
|
||||
if (process.isAlive()) {
|
||||
|
||||
log.warn("Command [ {} ] stuck for {} s, destroy the command process !", process.info().commandLine().get(), processMaxWaitSeconds);
|
||||
log.warn(
|
||||
"Command [ {} ] stuck for {} s, destroy the command process !",
|
||||
process
|
||||
.info()
|
||||
.commandLine()
|
||||
.get(),
|
||||
processMaxWaitSeconds
|
||||
);
|
||||
|
||||
// shutdown the process
|
||||
process.destroyForcibly();
|
||||
@@ -158,7 +189,9 @@ public class CommandExecutor {
|
||||
TimeUnit.SECONDS.sleep(1);
|
||||
|
||||
// clear the log Cache Thread scope
|
||||
logToArrayListCache.getExecutionCmdCachedLogArrayList(streamKey).clear();
|
||||
logToArrayListCache
|
||||
.getExecutionCmdCachedLogArrayList(streamKey)
|
||||
.clear();
|
||||
|
||||
// clear the stream sender
|
||||
streamSender.clearLocalCache(streamKey);
|
||||
|
||||
@@ -1,81 +0,0 @@
|
||||
package io.wdd.agent.executor.config;
|
||||
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Component
|
||||
public class FunctionReader {
|
||||
|
||||
|
||||
public List<List<String>> ReadFileToCommandList(String functionFilePath) {
|
||||
|
||||
// https://www.digitalocean.com/community/tutorials/java-read-file-line-by-line
|
||||
|
||||
List<List<String>> result = null;
|
||||
|
||||
try {
|
||||
|
||||
BufferedReader bufferedReader = new BufferedReader(new FileReader(functionFilePath));
|
||||
result = doReadContent(result, bufferedReader);
|
||||
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public List<List<String>> ReadStringToCommandList(String functionContent) {
|
||||
|
||||
List<List<String>> result = null;
|
||||
|
||||
try {
|
||||
|
||||
BufferedReader bufferedReader = new BufferedReader(new StringReader(functionContent));
|
||||
result = doReadContent(result, bufferedReader);
|
||||
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
private List<List<String>> doReadContent(List<List<String>> result, BufferedReader bufferedReader) throws IOException {
|
||||
String line = bufferedReader.readLine();
|
||||
|
||||
if (line != null) {
|
||||
result = new ArrayList<>(64);
|
||||
}
|
||||
|
||||
while (line != null) {
|
||||
if (!StringUtils.isEmpty(line)) {
|
||||
result.add(this.SplitLineToCommandList(line));
|
||||
}
|
||||
line = bufferedReader.readLine();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public List<String> SplitLineToCommandList(String commandLine) {
|
||||
|
||||
return Arrays.stream(commandLine.split(" ")).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user