diff --git a/src/main/java/com/browserstack/automate/ci/common/uploader/AppUploaderHelper.java b/src/main/java/com/browserstack/automate/ci/common/uploader/AppUploaderHelper.java index fcb3a5e9..cf23d8d3 100644 --- a/src/main/java/com/browserstack/automate/ci/common/uploader/AppUploaderHelper.java +++ b/src/main/java/com/browserstack/automate/ci/common/uploader/AppUploaderHelper.java @@ -12,6 +12,8 @@ import hudson.util.FormValidation; public class AppUploaderHelper { + private static final int MAX_RETRY_ATTEMPTS = 2; + private static final long RETRY_DELAY_MS = 1000; public static FormValidation validateAppPath(String appPath) { if (appPath == null || appPath.isEmpty()) { @@ -47,23 +49,36 @@ public static String uploadApp(Actionable build, PrintStream logger, String appP AppUploader appUploader = new AppUploader(appPath, credentials, customProxy, logger); String appId = ""; - try { - PluginLogger.log(logger, "Uploading app " + appPath + " to Browserstack."); - appId = appUploader.uploadFile(); - PluginLogger.log(logger, - appPath + " uploaded successfully to Browserstack with app_url : " + appId); - } catch (AppAutomateException e) { - PluginLogger.logError(logger, "App upload failed."); - PluginLogger.logError(logger, e.getMessage()); - return null; - } catch (InvalidFileExtensionException e) { - PluginLogger.logError(logger, e.getMessage()); - return null; - } catch (FileNotFoundException e) { - PluginLogger.logError(logger, e.getMessage()); - return null; + for (int attempt = 1; attempt <= MAX_RETRY_ATTEMPTS; attempt++) { + try { + PluginLogger.log(logger, String.format("Uploading app %s to Browserstack. Attempt %d of %d", appPath, attempt, MAX_RETRY_ATTEMPTS)); + appId = appUploader.uploadFile(); + PluginLogger.log(logger, + String.format("%s uploaded successfully to Browserstack with app_url : %s", appPath, appId)); + return appId; + } catch (AppAutomateException e) { + int statusCode = e.getStatusCode(); + PluginLogger.logError(logger, String.format("App upload failed with status code: %d. Attempt %d of %d", statusCode, attempt, MAX_RETRY_ATTEMPTS)); + PluginLogger.logError(logger, e.getMessage()); + if ((statusCode >= 500 || statusCode == 0) && attempt < MAX_RETRY_ATTEMPTS) { + PluginLogger.log(logger, String.format("Retrying in %d seconds...", RETRY_DELAY_MS / 1000)); + try { + Thread.sleep(RETRY_DELAY_MS); + } catch (InterruptedException ie) { + Thread.currentThread().interrupt(); + PluginLogger.log(logger, "Upload retry interrupted. Error: " + ie.getMessage()); + return null; + } + } else { + return null; + } + } catch (InvalidFileExtensionException | FileNotFoundException e) { + PluginLogger.logError(logger, e.getMessage()); + return null; + } } - return appId; + PluginLogger.logError(logger, String.format("App upload failed after %d attempts", MAX_RETRY_ATTEMPTS)); + return null; } }