* added retry { ... } construct

This commit is contained in:
Reinhard Pointner 2012-10-28 15:01:31 +00:00
parent a7822901f8
commit 99a2013153
1 changed files with 17 additions and 0 deletions

View File

@ -359,3 +359,20 @@ def tryQuietly(c) {
return null
}
}
/**
* Retry given closure until it returns successfully (indefinitely by default)
*/
def retry(n = -1, quiet = false, c) {
for(int i = 1; i <= n; i++) {
try {
return c.call()
} catch(Throwable e) {
if (i >= 0 && i >= n) {
throw e
} else if (!quiet) {
_log.warning("retry $i: ${e.class.simpleName}: ${e.message}")
}
}
}
}