Skip to content

Commit 37edac9

Browse files
authored
Bugfix: shutdown the agent if agent info has been used (#587)
* shutdown the agent if agent info has been used * Bugfix: use the constant to equal variable
1 parent 173279a commit 37edac9

File tree

10 files changed

+26
-23
lines changed

10 files changed

+26
-23
lines changed

agent/agent_installer/Windows/restartAgent.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ set newfile=%1
1717
::stop hydra lab agent service
1818
net stop "Hydra Lab Agent Service"
1919
::kill hydra lab agent java process
20-
::Powershell -Command "& {Get-WmiObject Win32_Process -Filter \"name like '%%java%%' and CommandLine like '%%agent%%'\" | Select-Object ProcessId -OutVariable pids;if(-not $pids -eq '' ) {stop-process -id $pids.ProcessId}}"
20+
Powershell -Command "& {Get-WmiObject Win32_Process -Filter \"name like '%%java%%' and CommandLine like '%%agent%%'\" | Select-Object ProcessId -OutVariable pids;if(-not $pids -eq '' ) {stop-process -id $pids.ProcessId}}"
2121
if "%newfile%"=="" ( echo "No need to update" ) else (
2222
if not exist "%newfile%" ( echo "%newfile% not exist" ) else (
2323
echo "Updating"

agent/agent_installer/Windows/restartAgent_WindowsService.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ set newfile=%1
1717
::stop hydra lab agent service
1818
net stop "Hydra Lab Agent Service"
1919
::kill hydra lab agent java process
20-
::Powershell -Command "& {Get-WmiObject Win32_Process -Filter \"name like '%%java%%' and CommandLine like '%%agent%%'\" | Select-Object ProcessId -OutVariable pids;if(-not $pids -eq '' ) {stop-process -id $pids.ProcessId}}"
20+
Powershell -Command "& {Get-WmiObject Win32_Process -Filter \"name like '%%java%%' and CommandLine like '%%agent%%'\" | Select-Object ProcessId -OutVariable pids;if(-not $pids -eq '' ) {stop-process -id $pids.ProcessId}}"
2121
if "%newfile%"=="" ( echo "No need to update" ) else (
2222
if not exist "%newfile%" ( echo "%newfile% not exist" ) else (
2323
echo "Updating"

agent/agent_installer/Windows/restartAgent_WindowsTaskScheduler.bat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ echo newfile = %1
1616
set newfile=%1
1717
::stop hydra lab agent service
1818
::net stop "Hydra Lab Agent Service"
19-
kill hydra lab agent java process
20-
::Powershell -Command "& {Get-WmiObject Win32_Process -Filter \"name like '%%java%%' and CommandLine like '%%agent%%'\" | Select-Object ProcessId -OutVariable pids;if(-not $pids -eq '' ) {stop-process -id $pids.ProcessId}}"
19+
::kill hydra lab agent java process
20+
Powershell -Command "& {Get-WmiObject Win32_Process -Filter \"name like '%%java%%' and CommandLine like '%%agent%%'\" | Select-Object ProcessId -OutVariable pids;if(-not $pids -eq '' ) {stop-process -id $pids.ProcessId}}"
2121
if "%newfile%"=="" ( echo "No need to update" ) else (
2222
if not exist "%newfile%" ( echo "%newfile% not exist" ) else (
2323
echo "Updating"

agent/src/main/java/com/microsoft/hydralab/agent/scheduled/ScheduledDeviceControlTasks.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ public void scheduledCheckWebSocketConnection() {
3636
if (agentWebSocketClient.isConnectionActive()) {
3737
return;
3838
}
39-
if (!agentWebSocketClient.shouldRetryConnection()) {
40-
return;
41-
}
4239
logger.info("Try reconnecting the WS server");
4340
agentWebSocketClient.reconnect();
4441
}

agent/src/main/java/com/microsoft/hydralab/agent/service/TestTaskEngineService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ protected Set<TestRunDevice> chooseDevices(TestTask testTask) {
135135
}
136136

137137
String runningType = testTask.getRunningType();
138-
if (((runningType.equals(TestTask.TestRunningType.APPIUM_CROSS)) || (runningType.equals(TestTask.TestRunningType.T2C_JSON_TEST))) && devices.size() > 1) {
138+
if (((TestTask.TestRunningType.APPIUM_CROSS.equals(runningType)) || (TestTask.TestRunningType.T2C_JSON_TEST.equals(runningType))) && devices.size() > 1) {
139139
Optional<DeviceInfo> mainDeviceInfo = devices.stream().filter(deviceInfo -> !DeviceType.WINDOWS.name().equals(deviceInfo.getType())).findFirst();
140140
Assert.isTrue(mainDeviceInfo.isPresent(), "There are more than 1 device, but all of them is windows device!");
141141
devices.remove(mainDeviceInfo.get());

agent/src/main/java/com/microsoft/hydralab/agent/socket/AgentWebSocketClient.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.microsoft.hydralab.agent.service.AgentWebSocketClientService;
77
import com.microsoft.hydralab.common.entity.common.Message;
88
import com.microsoft.hydralab.common.util.Const;
9+
import com.microsoft.hydralab.common.util.FlowUtil;
910
import com.microsoft.hydralab.common.util.SerializeUtil;
1011
import lombok.extern.slf4j.Slf4j;
1112
import org.java_websocket.client.WebSocketClient;
@@ -20,7 +21,6 @@ public class AgentWebSocketClient extends WebSocketClient {
2021
private final AgentWebSocketClientService agentWebSocketClientService;
2122

2223
private boolean connectionActive = false;
23-
private boolean shouldRetryConnection = true;
2424
private int reconnectTime = 0;
2525

2626
public AgentWebSocketClient(URI serverUri, AgentWebSocketClientService agentWebSocketClientService) {
@@ -29,7 +29,14 @@ public AgentWebSocketClient(URI serverUri, AgentWebSocketClientService agentWebS
2929
agentWebSocketClientService.setSendMessageCallback(message -> {
3030
byte[] data = SerializeUtil.messageToByteArr(message);
3131
log.info("send, path: {}, message data len: {}", message.getPath(), data.length);
32-
AgentWebSocketClient.this.send(data);
32+
try {
33+
FlowUtil.retryAndSleepWhenException(3, 10, () -> {
34+
AgentWebSocketClient.this.send(data);
35+
return true;
36+
});
37+
} catch (Exception e) {
38+
log.error("send message to center error, message path is {}", message.getPath(), e);
39+
}
3340
});
3441
}
3542

@@ -58,11 +65,13 @@ public void onMessage(String message) {
5865

5966
@Override
6067
public void onClose(int code, String reason, boolean remote) {
61-
log.info("onClose {}, {}, {}", code, reason, remote);
68+
log.error("onClose {}, {}, {}", code, reason, remote);
6269
reconnectTime++;
6370
connectionActive = false;
64-
// The remote server has stopped, we need to try reconnecting at certain frequency.
65-
shouldRetryConnection = code != CloseReason.CloseCodes.CANNOT_ACCEPT.getCode();
71+
// if the connection is closed by server with 1008,1003, exit the agent
72+
if (code == CloseReason.CloseCodes.CANNOT_ACCEPT.getCode() || code == CloseReason.CloseCodes.VIOLATED_POLICY.getCode()) {
73+
System.exit(code);
74+
}
6675
}
6776

6877
@Override
@@ -76,10 +85,6 @@ public boolean isConnectionActive() {
7685
return connectionActive;
7786
}
7887

79-
public boolean shouldRetryConnection() {
80-
return shouldRetryConnection;
81-
}
82-
8388
public int getReconnectTime() {
8489
return reconnectTime;
8590
}

center/src/main/java/com/microsoft/hydralab/center/controller/DeviceGroupController.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.springframework.http.HttpStatus;
2222
import org.springframework.http.MediaType;
2323
import org.springframework.security.core.annotation.CurrentSecurityContext;
24+
import org.springframework.util.StringUtils;
2425
import org.springframework.web.bind.annotation.GetMapping;
2526
import org.springframework.web.bind.annotation.PostMapping;
2627
import org.springframework.web.bind.annotation.RequestMapping;
@@ -61,7 +62,7 @@ public Result<DeviceGroup> createGroup(@CurrentSecurityContext SysUser requestor
6162
if (team == null) {
6263
return Result.error(HttpStatus.BAD_REQUEST.value(), "Team doesn't exist.");
6364
}
64-
if (groupName == null || "".equals(groupName)) {
65+
if (StringUtils.isEmpty(groupName)) {
6566
return Result.error(HttpStatus.BAD_REQUEST.value(), "groupName is required");
6667
}
6768
if (deviceGroupService.isGroupNameIllegal(groupName)) {

center/src/main/java/com/microsoft/hydralab/center/service/DeviceAgentManagementService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,13 @@ public void onMessage(Message message, @NotNull Session session) throws IOExcept
189189
AgentUser agentUser = searchQualifiedAgent(message);
190190
if (agentUser == null) {
191191
log.warn("Session {} is not registered agent, associated agent {}", session.getId(), message.getBody());
192-
session.close(new CloseReason(CloseReason.CloseCodes.CANNOT_ACCEPT, "Not permitted"));
192+
session.close(new CloseReason(CloseReason.CloseCodes.CANNOT_ACCEPT, "Agent Info is not correct!"));
193193
return;
194194
}
195195

196196
if (agentDeviceGroups.get(agentUser.getId()) != null && checkIsSessionAliveByAgentId(agentUser.getId())) {
197197
log.warn("Session {} is already connected under another agent, associated agent {}", session.getId(), message.getBody());
198-
session.close(new CloseReason(CloseReason.CloseCodes.VIOLATED_POLICY, "AgentID has been used"));
198+
session.close(new CloseReason(CloseReason.CloseCodes.VIOLATED_POLICY, "AgentID has been used!"));
199199
return;
200200
}
201201

center/src/main/java/com/microsoft/hydralab/center/socket/CenterDeviceSocketEndpoint.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void onMessage(ByteBuffer message, Session session) {
5454
formattedMessage = SerializeUtil.byteArrToMessage(message.array());
5555
} catch (Exception e) {
5656
try {
57-
session.close(new CloseReason(CloseReason.CloseCodes.CANNOT_ACCEPT, "Message format error"));
57+
session.close(new CloseReason(CloseReason.CloseCodes.CANNOT_ACCEPT, "Message format error, please update your agent."));
5858
} catch (IOException ex) {
5959
throw new RuntimeException(ex);
6060
}

common/src/main/java/com/microsoft/hydralab/common/management/device/impl/AndroidDeviceDriver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public void deviceConnected(IDevice device) {
9090
if (deviceInfo == null) {
9191
return;
9292
}
93-
if (device.getState().equals(DeviceState.ONLINE)) {
93+
if (DeviceState.ONLINE.equals(device.getState())) {
9494
agentManagementService.getDeviceStatusListenerManager().onDeviceConnected(deviceInfo);
9595
} else {
9696
agentManagementService.getDeviceStatusListenerManager().onDeviceInactive(deviceInfo);
@@ -133,7 +133,7 @@ public void deviceChanged(IDevice device, int changeMask) {
133133
return;
134134
}
135135

136-
if (device.getState().equals(DeviceState.ONLINE)) {
136+
if (DeviceState.ONLINE.equals(device.getState())) {
137137
agentManagementService.getDeviceStatusListenerManager().onDeviceConnected(deviceInfo);
138138
} else {
139139
agentManagementService.getDeviceStatusListenerManager().onDeviceInactive(deviceInfo);

0 commit comments

Comments
 (0)