From f44aac84f3527a27e3657c97d34d670e0812a3ba Mon Sep 17 00:00:00 2001 From: Luennng Date: Thu, 23 May 2024 12:04:53 +0800 Subject: [PATCH] [optimize](regression)Add retry for curl request --- .../plugins/plugin_curl_requester.groovy | 32 ++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/regression-test/plugins/plugin_curl_requester.groovy b/regression-test/plugins/plugin_curl_requester.groovy index 7ea6f438fb7dc9..6c4bc86270f4c1 100644 --- a/regression-test/plugins/plugin_curl_requester.groovy +++ b/regression-test/plugins/plugin_curl_requester.groovy @@ -32,13 +32,37 @@ Suite.metaClass.curl = { String method, String url /* param */-> } Integer timeout = 10; // 10 seconds; + Integer maxRetries = 10; // Maximum number of retries + Integer retryCount = 0; // Current retry count + Integer sleepTime = 5000; // Sleep time in milliseconds String cmd = String.format("curl --max-time %d -X %s %s", timeout, method, url).toString() logger.info("curl cmd: " + cmd) - def process = cmd.execute() - int code = process.waitFor() - String err = IOGroovyMethods.getText(new BufferedReader(new InputStreamReader(process.getErrorStream()))) - String out = process.getText() + def process + int code + String err + String out + + while (retryCount < maxRetries) { + process = cmd.execute() + code = process.waitFor() + err = IOGroovyMethods.getText(new BufferedReader(new InputStreamReader(process.getErrorStream()))) + out = process.getText() + + // If the command was successful, break the loop + if (code == 0) { + break + } + + // If the command was not successful, increment the retry count, sleep for a while and try again + retryCount++ + sleep(sleepTime) + } + + // If the command was not successful after maxRetries attempts, log the failure and return the result + if (code != 0) { + logger.error("Command curl failed after " + maxRetries + " attempts. code: " + code + ", err: " + err) + } return [code, out, err] }