|
12 | 12 | import hudson.util.FormValidation; |
13 | 13 |
|
14 | 14 | public class AppUploaderHelper { |
| 15 | + private static final int MAX_RETRY_ATTEMPTS = 2; |
| 16 | + private static final long RETRY_DELAY_MS = 1000; |
15 | 17 |
|
16 | 18 | public static FormValidation validateAppPath(String appPath) { |
17 | 19 | if (appPath == null || appPath.isEmpty()) { |
@@ -47,23 +49,36 @@ public static String uploadApp(Actionable build, PrintStream logger, String appP |
47 | 49 |
|
48 | 50 | AppUploader appUploader = new AppUploader(appPath, credentials, customProxy, logger); |
49 | 51 | 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 | + } |
65 | 79 | } |
66 | | - return appId; |
| 80 | + PluginLogger.logError(logger, String.format("App upload failed after %d attempts", MAX_RETRY_ATTEMPTS)); |
| 81 | + return null; |
67 | 82 | } |
68 | 83 |
|
69 | 84 | } |
0 commit comments