Skip to content

Commit

Permalink
Fix pre-install app bug when device reboot. (#266)
Browse files Browse the repository at this point in the history
* fix pre install app bug

* update shoudown policy
  • Loading branch information
zhou9584 committed Feb 14, 2023
1 parent 7395cc4 commit 40c8280
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.microsoft.hydralab.common.management.listener.impl.DeviceStabilityMonitor;
import com.microsoft.hydralab.common.monitor.MetricPushGateway;
import com.microsoft.hydralab.common.util.ADBOperateUtil;
import com.microsoft.hydralab.common.util.Const;
import com.microsoft.hydralab.common.util.blob.BlobStorageClient;
import io.micrometer.core.instrument.MeterRegistry;
import io.prometheus.client.CollectorRegistry;
Expand Down Expand Up @@ -65,6 +66,8 @@ public class AppConfiguration {
private long deviceStateChangeRecoveryTime;
@Value("${app.adb.host:}")
private String adbServerHost;
@Value("${app.pre-install.shutdown-if-fail:true}")
private Boolean shutdownIfFail;
@Value("${app.appium.host:}")
private String appiumServerHost;

Expand Down Expand Up @@ -116,6 +119,7 @@ public DeviceManager initDeviceManager(BlobStorageClient deviceLabBlobClient, AD
}
}
deviceManager.setPreAppDir(preAppDir);
deviceManager.setPreInstallPolicy(shutdownIfFail ? Const.PreInstallPolicy.SHUTDOWN : Const.PreInstallPolicy.IGNORE);
deviceManager.setDeviceStatusListenerManager(deviceStatusListenerManager);
deviceManager.setTestBaseDirUrlMapping(AppOptions.TEST_CASE_RESULT_STORAGE_MAPPING_REL_PATH);
File deviceLogBaseDir = new File(appOptions.getDeviceLogStorageLocation());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ public abstract class DeviceManager {
protected BlobStorageClient blobStorageClient;
protected File testBaseDir;
protected File preAppDir;
protected String preInstallPolicy;

public String getPreInstallPolicy() {
return preInstallPolicy;
}

public void setPreInstallPolicy(String preInstallPolicy) {
this.preInstallPolicy = preInstallPolicy;
}

protected String testBaseDirUrlMapping;
protected File deviceLogBaseDir;
protected File screenshotDir;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ public void deviceConnected(IDevice device) {
if (deviceInfo == null) {
return;
}
deviceStatusListenerManager.onDeviceConnected(deviceInfo);
if (device.getState().equals(DeviceState.ONLINE)) {
deviceStatusListenerManager.onDeviceConnected(deviceInfo);
} else {
deviceStatusListenerManager.onDeviceInactive(deviceInfo);
}
}

@Override
Expand Down Expand Up @@ -102,10 +106,10 @@ public void deviceChanged(IDevice device, int changeMask) {
return;
}

if (device.getState() != DeviceState.ONLINE) {
deviceStatusListenerManager.onDeviceInactive(deviceInfo);
} else {
if (device.getState().equals(DeviceState.ONLINE)) {
deviceStatusListenerManager.onDeviceConnected(deviceInfo);
} else {
deviceStatusListenerManager.onDeviceInactive(deviceInfo);
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
// Licensed under the MIT License.
package com.microsoft.hydralab.common.management.listener.impl;

import com.android.ddmlib.InstallException;
import com.microsoft.hydralab.common.entity.common.DeviceInfo;
import com.microsoft.hydralab.common.management.DeviceManager;
import com.microsoft.hydralab.common.management.listener.DeviceStatusListener;
import com.microsoft.hydralab.common.util.Const;
import com.microsoft.hydralab.common.util.FlowUtil;
import com.microsoft.hydralab.common.util.HydraLabRuntimeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -40,12 +41,17 @@ public void onDeviceConnected(DeviceInfo deviceInfo) {
continue;
}
try {
deviceManager.installApp(deviceInfo, appFile.getAbsolutePath(), classLogger);
} catch (InstallException e) {
FlowUtil.retryAndSleepWhenFalse(3, 10, () -> deviceManager.installApp(deviceInfo, appFile.getAbsolutePath(), classLogger));
classLogger.info("Pre-Install {} successfully", appFile.getAbsolutePath());
break;
} catch (Exception e) {
String errorMessage = String.format("Pre-Install %s failed", appFile.getAbsolutePath());
classLogger.error(errorMessage, e);
throw new HydraLabRuntimeException(HttpStatus.INTERNAL_SERVER_ERROR.value(), errorMessage, e);
if (Const.PreInstallPolicy.SHUTDOWN.equals(deviceManager.getPreInstallPolicy())) {
throw new HydraLabRuntimeException(HttpStatus.INTERNAL_SERVER_ERROR.value(), errorMessage, e);
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,9 @@ interface AuthComponent {
// permission
String AUTHORITY = "AUTHORITY";
}

interface PreInstallPolicy{
String SHUTDOWN = "SHUTDOWN";
String IGNORE = "IGNORE";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,23 @@ public static boolean retryWhenFalse(int count, Callable<Boolean> predicate) {
}
return false;
}

public static boolean retryAndSleepWhenFalse(int count, int sleepSeconds, Callable<Boolean> predicate) throws Exception {
Exception toThrow = null;
while (count > 0) {
try {
if (predicate.call()) {
return true;
}
} catch (Exception e) {
toThrow = e;
}
ThreadUtils.safeSleep(sleepSeconds * 1000);
count--;
}
if (toThrow != null) {
throw toThrow;
}
return false;
}
}

0 comments on commit 40c8280

Please sign in to comment.