forked from nim-lang/Nim
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix #266 retry on failure to avoid common 503 github errors
- Loading branch information
1 parent
e5b64af
commit 34ca1c6
Showing
2 changed files
with
36 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
##[ | ||
internal API for now, API subject to change | ||
]## | ||
|
||
import std/[os,osproc,sugar,strutils] | ||
|
||
proc actionRetry*(maxRetry: int, backoffDuration: float, action: proc(): bool): bool = | ||
## retry `action` up to `maxRetry` times with exponential backoff and initial | ||
## duraton of `backoffDuration` seconds | ||
var t = backoffDuration | ||
for i in 0..<maxRetry: | ||
if action(): return true | ||
if i == maxRetry - 1: break | ||
sleep(int(t * 1000)) | ||
t = t * 2 # exponential backoff | ||
return false | ||
|
||
proc nimbleInstall*(name: string, message: var string): bool = | ||
let cmd = "nimble install -y " & name | ||
let (outp, status) = execCmdEx(cmd) | ||
if status != 0: | ||
message = "'$1' failed:\n$2" % [cmd, outp] | ||
result = false | ||
else: result = true | ||
|
||
when isMainModule: | ||
block: | ||
var msg: string | ||
let ok = actionRetry(maxRetry = 2, backoffDuration = 0.1): | ||
(proc(): bool = nimbleInstall("nonexistant", msg)) | ||
doAssert "Package not found" in msg | ||
doAssert not ok |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters