Skip to content

Commit 5a462a8

Browse files
abhinvv1francisf
authored andcommitted
add retries in app uploading
1 parent 647657f commit 5a462a8

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

src/main/java/com/browserstack/automate/ci/common/uploader/AppUploaderHelper.java

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import hudson.util.FormValidation;
1313

1414
public class AppUploaderHelper {
15+
private static final int MAX_RETRY_ATTEMPTS = 2;
16+
private static final long RETRY_DELAY_MS = 1000;
1517

1618
public static FormValidation validateAppPath(String appPath) {
1719
if (appPath == null || appPath.isEmpty()) {
@@ -47,23 +49,36 @@ public static String uploadApp(Actionable build, PrintStream logger, String appP
4749

4850
AppUploader appUploader = new AppUploader(appPath, credentials, customProxy, logger);
4951
String appId = "";
50-
try {
51-
PluginLogger.log(logger, "Uploading app " + appPath + " to Browserstack.");
52-
appId = appUploader.uploadFile();
53-
PluginLogger.log(logger,
54-
appPath + " uploaded successfully to Browserstack with app_url : " + appId);
55-
} catch (AppAutomateException e) {
56-
PluginLogger.logError(logger, "App upload failed.");
57-
PluginLogger.logError(logger, e.getMessage());
58-
return null;
59-
} catch (InvalidFileExtensionException e) {
60-
PluginLogger.logError(logger, e.getMessage());
61-
return null;
62-
} catch (FileNotFoundException e) {
63-
PluginLogger.logError(logger, e.getMessage());
64-
return null;
52+
for (int attempt = 1; attempt <= MAX_RETRY_ATTEMPTS; attempt++) {
53+
try {
54+
PluginLogger.log(logger, String.format("Uploading app %s to Browserstack. Attempt %d of %d", appPath, attempt, MAX_RETRY_ATTEMPTS));
55+
appId = appUploader.uploadFile();
56+
PluginLogger.log(logger,
57+
String.format("%s uploaded successfully to Browserstack with app_url : %s", appPath, appId));
58+
return appId;
59+
} catch (AppAutomateException e) {
60+
PluginLogger.logError(logger, String.format("App upload failed with status code: %d. Attempt %d of %d", e.getStatusCode(), attempt, MAX_RETRY_ATTEMPTS));
61+
PluginLogger.logError(logger, e.getMessage());
62+
if (attempt < MAX_RETRY_ATTEMPTS) {
63+
PluginLogger.log(logger, String.format("Retrying in %d seconds...", RETRY_DELAY_MS / 1000));
64+
try {
65+
Thread.sleep(RETRY_DELAY_MS);
66+
} catch (InterruptedException ie) {
67+
Thread.currentThread().interrupt();
68+
PluginLogger.log(logger, "Upload retry interrupted. Error: " + ie.getMessage());
69+
return null;
70+
}
71+
}
72+
} catch (InvalidFileExtensionException e) {
73+
PluginLogger.logError(logger, e.getMessage());
74+
return null;
75+
} catch (FileNotFoundException e) {
76+
PluginLogger.logError(logger, e.getMessage());
77+
return null;
78+
}
6579
}
66-
return appId;
80+
PluginLogger.logError(logger, String.format("App upload failed after %d attempts", MAX_RETRY_ATTEMPTS));
81+
return null;
6782
}
6883

6984
}

0 commit comments

Comments
 (0)