|
5 | 5 | import java.util.List; |
6 | 6 | import java.util.Map; |
7 | 7 | import java.util.Objects; |
| 8 | +import java.util.concurrent.TimeUnit; |
8 | 9 | import javax.annotation.PostConstruct; |
9 | 10 | import org.hzero.websocket.constant.ClientWebSocketConstant; |
10 | 11 | import org.hzero.websocket.vo.MsgVO; |
|
39 | 40 | public class HostAgentSocketHandler extends AbstractSocketHandler { |
40 | 41 |
|
41 | 42 | private static final Logger LOGGER = LoggerFactory.getLogger(HostAgentSocketHandler.class); |
| 43 | + private static final String C7N_AGENT_UPGRADE_COUNT_REDIS_KEY = "host:%s"; |
| 44 | + private static final Integer C7N_AGENT_MAX_UPGRADE_ATTEMPT_COUNT = 3; |
42 | 45 |
|
43 | 46 | private final Map<String, HostMsgHandler> hostMsgHandlerMap = new HashMap<>(); |
44 | 47 |
|
@@ -79,18 +82,32 @@ public void afterConnectionEstablished(WebSocketSession session) { |
79 | 82 | hostSessionVO.setRegisterKey(WebSocketTool.getGroup(session)); |
80 | 83 | redisTemplate.opsForHash().put(DevopsHostConstants.HOST_SESSION, hostSessionVO.getRegisterKey(), hostSessionVO); |
81 | 84 |
|
82 | | - MsgVO msgVO = new MsgVO(); |
| 85 | + MsgVO msgVO; |
83 | 86 | // 版本不一致,需要升级 |
84 | 87 | if (!agentVersion.equals(WebSocketTool.getVersion(session))) { |
85 | | - DevopsHostDTO devopsHostDTO = devopsHostService.baseQuery(Long.parseLong(hostId)); |
86 | | - HostMsgVO hostMsgVO = new HostMsgVO(); |
87 | | - hostMsgVO.setType(HostCommandEnum.UPGRADE_AGENT.value()); |
88 | | - Map<String, String> upgradeInfo = new HashMap<>(); |
89 | | - upgradeInfo.put("upgradeCommand", devopsHostService.queryShell(devopsHostDTO.getProjectId(), devopsHostDTO.getId(), true)); |
90 | | - upgradeInfo.put("version", agentVersion); |
91 | | - hostMsgVO.setPayload(JsonHelper.marshalByJackson(upgradeInfo)); |
92 | | - |
93 | | - msgVO = (new MsgVO()).setGroup(DevopsHostConstants.GROUP + hostId).setKey(HostCommandEnum.UPGRADE_AGENT.value()).setMessage(JsonHelper.marshalByJackson(hostMsgVO)).setType(ClientWebSocketConstant.SendType.S_GROUP); |
| 88 | + String redisKey = String.format(C7N_AGENT_UPGRADE_COUNT_REDIS_KEY, hostId); |
| 89 | + Integer count = (Integer) redisTemplate.opsForValue().get(redisKey); |
| 90 | + if (C7N_AGENT_MAX_UPGRADE_ATTEMPT_COUNT.equals(count)) { |
| 91 | + // 表示agent进行了3次尝试升级,都失败了,那么agent应该退出。手动处理升级失败问题 |
| 92 | + HostMsgVO hostMsgVO = new HostMsgVO(); |
| 93 | + hostMsgVO.setType(HostCommandEnum.EXIT_AGENT.value()); |
| 94 | + hostMsgVO.setPayload("{}"); |
| 95 | + msgVO = (new MsgVO()).setGroup(DevopsHostConstants.GROUP + hostId).setKey(HostCommandEnum.EXIT_AGENT.value()).setMessage(JsonHelper.marshalByJackson(hostMsgVO)).setType(ClientWebSocketConstant.SendType.S_GROUP); |
| 96 | + } else { |
| 97 | + if (count == null) { |
| 98 | + count = 0; |
| 99 | + } |
| 100 | + count++; |
| 101 | + redisTemplate.opsForValue().set(redisKey, count, 1800, TimeUnit.SECONDS); |
| 102 | + DevopsHostDTO devopsHostDTO = devopsHostService.baseQuery(Long.parseLong(hostId)); |
| 103 | + HostMsgVO hostMsgVO = new HostMsgVO(); |
| 104 | + hostMsgVO.setType(HostCommandEnum.UPGRADE_AGENT.value()); |
| 105 | + Map<String, String> upgradeInfo = new HashMap<>(); |
| 106 | + upgradeInfo.put("upgradeCommand", devopsHostService.queryShell(devopsHostDTO.getProjectId(), devopsHostDTO.getId(), true)); |
| 107 | + upgradeInfo.put("version", agentVersion); |
| 108 | + hostMsgVO.setPayload(JsonHelper.marshalByJackson(upgradeInfo)); |
| 109 | + msgVO = (new MsgVO()).setGroup(DevopsHostConstants.GROUP + hostId).setKey(HostCommandEnum.UPGRADE_AGENT.value()).setMessage(JsonHelper.marshalByJackson(hostMsgVO)).setType(ClientWebSocketConstant.SendType.S_GROUP); |
| 110 | + } |
94 | 111 | } else { |
95 | 112 | HostMsgVO hostMsgVO = new HostMsgVO(); |
96 | 113 | hostMsgVO.setType(HostCommandEnum.INIT_AGENT.value()); |
|
0 commit comments