Skip to content

Commit 393b7cb

Browse files
Add simple retry mechanism to uploadBackups task
1 parent 97d885a commit 393b7cb

File tree

1 file changed

+40
-21
lines changed

1 file changed

+40
-21
lines changed

build.gradle

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -226,29 +226,48 @@ task uploadBackups {
226226
def shortVersion = getGhVersion().substring(1)
227227
def backupUrl = "https://api.wurstclient.net/artifact-backups/Wurst/${shortVersion}"
228228

229-
def connection = new URL(backupUrl).openConnection() as HttpURLConnection
230-
def boundary = UUID.randomUUID().toString()
231-
connection.setRequestMethod("POST")
232-
connection.setRequestProperty("X-API-Key", ENV.WI_BACKUPS_API_KEY)
233-
connection.setRequestProperty("Accept", "application/json")
234-
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=$boundary")
235-
connection.doOutput = true
229+
def maxRetries = 3
230+
def retryCount = 0
231+
def success = false
236232

237-
def output = connection.outputStream
238-
[remapJar, remapSourcesJar].each { jarTask ->
239-
def file = jarTask.archiveFile.get().asFile
240-
output << "--${boundary}\r\n"
241-
output << "Content-Disposition: form-data; name=\"files\"; filename=\"${file.name}\"\r\n"
242-
output << "Content-Type: application/java-archive\r\n\r\n"
243-
file.withInputStream { input ->
244-
output << input
233+
while (!success && retryCount < maxRetries) {
234+
try {
235+
def connection = new URL(backupUrl).openConnection() as HttpURLConnection
236+
def boundary = UUID.randomUUID().toString()
237+
connection.setRequestMethod("POST")
238+
connection.setRequestProperty("X-API-Key", ENV.WI_BACKUPS_API_KEY)
239+
connection.setRequestProperty("Accept", "application/json")
240+
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=$boundary")
241+
connection.doOutput = true
242+
243+
def output = connection.outputStream
244+
[remapJar, remapSourcesJar].each { jarTask ->
245+
def file = jarTask.archiveFile.get().asFile
246+
output << "--${boundary}\r\n"
247+
output << "Content-Disposition: form-data; name=\"files\"; filename=\"${file.name}\"\r\n"
248+
output << "Content-Type: application/java-archive\r\n\r\n"
249+
file.withInputStream { input ->
250+
output << input
251+
}
252+
output << "\r\n"
253+
}
254+
output << "--${boundary}--\r\n"
255+
output.flush()
256+
257+
if(connection.responseCode != 200) {
258+
throw new IOException("HTTP ${connection.responseCode}: ${connection.responseMessage}")
259+
}
260+
261+
success = true
262+
263+
} catch (Exception e) {
264+
retryCount++
265+
if (retryCount >= maxRetries) {
266+
throw new GradleException("Failed to upload backups after ${maxRetries} attempts: ${e.message}")
267+
}
268+
println "Upload attempt ${retryCount} failed: ${e.message}. Retrying in 5 seconds..."
269+
Thread.sleep(5000)
245270
}
246-
output << "\r\n"
247271
}
248-
output << "--${boundary}--\r\n"
249-
output.flush()
250-
251-
if(connection.responseCode != 200)
252-
throw new GradleException("Failed to upload backups: ${connection.responseCode} ${connection.responseMessage}")
253272
}
254273
}

0 commit comments

Comments
 (0)