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.handler.OMessageReplayContent; 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 java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import static io.wdd.rpc.message.handler.OMessageHandler.*; 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 SendCommandToAgent(String agentTopicName, String type, List funcContent, List commandList, List> commandListComplete, boolean needResultReplay, String resultKey, boolean durationTask) { ArrayList 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); log.debug( "发送的 matchKey为 {}, 内容为 {}", GenerateOMessageMatchKey( octopusMessage.getOctopusMessageType(), octopusMessage.getInit_time() ), octopusMessage ); // 需要返回结果 if (!durationTask) { // 等待结果 OMessageReplayContent replayContent = WaitFromAgent( octopusMessage, 1 ); CountDownLatch replayLatch = replayContent.getCountDownLatch(); boolean waitOK = false; try { waitOK = replayLatch.await( 10, TimeUnit.SECONDS ); } catch (InterruptedException e) { throw new RuntimeException(e); } finally { // 释放等待队列 StopWaitingResult(octopusMessage); // 转换结果 commandResultLog = (ArrayList) octopusMessage.getResult(); // debug log.debug( "执行命令 {} {} 在规定时间内结束, 结果为 {} 返回内容为 {}", executionMessage.getSingleLineCommand() == null ? executionMessage.getMultiLineCommand() : executionMessage.getSingleLineCommand(), waitOK ? "已经" : "未", 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 commandList, String resultKey, List> 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; } }