[ Service ] [ Executor ] 初步重构Executor部分的代码
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
package io.wdd.rpc.execute;
|
||||
|
||||
import io.wdd.common.utils.TimeUtils;
|
||||
import io.wdd.rpc.message.OctopusMessage;
|
||||
import io.wdd.rpc.message.OctopusMessageType;
|
||||
import io.wdd.rpc.message.sender.OMessageToAgentSender;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static io.wdd.rpc.status.CommonAndStatusCache.ALL_AGENT_TOPIC_NAME_SET;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class ExecutionServiceImpl implements ExecutionService {
|
||||
|
||||
private static final String MANUAL_COMMAND_TYPE = "manual-command";
|
||||
|
||||
@Resource
|
||||
OMessageToAgentSender oMessageToAgentSender;
|
||||
|
||||
@Override
|
||||
public ArrayList<String> SendCommandToAgent(String agentTopicName, String type, List<String> funcContent, List<String> commandList, List<List<String>> commandListComplete, boolean needResultReplay, String resultKey, boolean durationTask) {
|
||||
|
||||
ArrayList<String> commandResultLog = null;
|
||||
|
||||
// 归一化type
|
||||
if (StringUtils.isEmpty(type)) {
|
||||
type = MANUAL_COMMAND_TYPE;
|
||||
}
|
||||
|
||||
// 构造 Execution Command对应的消息体
|
||||
ExecutionMessage executionMessage = this
|
||||
.generateExecutionMessage(
|
||||
type,
|
||||
commandList,
|
||||
resultKey,
|
||||
commandListComplete,
|
||||
needResultReplay,
|
||||
durationTask
|
||||
);
|
||||
OctopusMessage octopusMessage = this.generateOctopusMessage(
|
||||
agentTopicName,
|
||||
executionMessage
|
||||
);
|
||||
|
||||
// send the message
|
||||
oMessageToAgentSender.send(octopusMessage);
|
||||
|
||||
|
||||
// 需要返回结果
|
||||
if (!durationTask) {
|
||||
|
||||
synchronized (octopusMessage) {
|
||||
try {
|
||||
octopusMessage.wait(10000);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
// 转换结果
|
||||
commandResultLog = (ArrayList<String>) octopusMessage.getResult();
|
||||
|
||||
// debug
|
||||
log.debug(
|
||||
"执行命令 {} 的结果为 {} 内容为 {}",
|
||||
executionMessage.getSingleLineCommand() == null ? executionMessage.getMultiLineCommand() : executionMessage.getSingleLineCommand(),
|
||||
octopusMessage.getResultCode(),
|
||||
octopusMessage.getResult()
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
return commandResultLog;
|
||||
}
|
||||
|
||||
|
||||
private OctopusMessage generateOctopusMessage(String agentTopicName, ExecutionMessage executionMessage) {
|
||||
|
||||
return OctopusMessage
|
||||
.builder()
|
||||
.octopusMessageType(OctopusMessageType.EXECUTOR)
|
||||
.init_time(TimeUtils.currentFormatTime())
|
||||
.uuid(agentTopicName)
|
||||
.content(
|
||||
executionMessage
|
||||
)
|
||||
.build();
|
||||
}
|
||||
|
||||
private ExecutionMessage generateExecutionMessage(String type, List<String> commandList, String resultKey, List<List<String>> commandListComplete, boolean needResultReplay, boolean durationTask) {
|
||||
|
||||
return ExecutionMessage
|
||||
.builder()
|
||||
.resultKey(resultKey)
|
||||
.type(type)
|
||||
.singleLineCommand(commandList)
|
||||
.multiLineCommand(commandListComplete)
|
||||
.needResultReplay(needResultReplay)
|
||||
.durationTask(durationTask)
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
private boolean validateCommandInfo(String agentTopicName, String type) {
|
||||
|
||||
// 检查agentTopicName是否存在
|
||||
if (!ALL_AGENT_TOPIC_NAME_SET.contains(agentTopicName)) {
|
||||
log.error(
|
||||
"agentTopicName异常! 输入为 => {}",
|
||||
agentTopicName
|
||||
);
|
||||
return false;
|
||||
//throw new MyRuntimeException("agentTopicName异常!" + agentTopicName);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user