Skip to content

Commit

Permalink
fix timotheecour#266 retry on failure to avoid common 503 github errors
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Jun 2, 2020
1 parent e5b64af commit f7bb764
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
32 changes: 32 additions & 0 deletions lib/std/private/nimbleutils.nim
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
7 changes: 4 additions & 3 deletions testament/categories.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# included from testament.nim

import important_packages
import std/private/nimbleutils

const
specialCategories = [
Expand Down Expand Up @@ -501,9 +502,9 @@ proc testNimblePackages(r: var TResults; cat: Category; packageFilter: string, p
if not existsDir(buildPath):
if hasDep:
let installName = if url.len != 0: url else: name
let (nimbleCmdLine, nimbleOutput, nimbleStatus) = execCmdEx2("nimble", ["install", "-y", installName])
if nimbleStatus != QuitSuccess:
let message = "nimble install failed:\n$ " & nimbleCmdLine & "\n" & nimbleOutput
var message: string
if not actionRetry(maxRetry = 3, backoffDuration = 1.0,
(proc(): bool = nimbleInstall(installName, message))):
r.addResult(test, targetC, "", message, reInstallFailed)
continue

Expand Down
4 changes: 4 additions & 0 deletions testament/lib/stdtest/netutils.nim
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
##[
internal API for now, API subject to change
]##

import std/[nativesockets, asyncdispatch, os]

proc bindAvailablePort*(handle: SocketHandle, port = Port(0)): Port =
Expand Down

0 comments on commit f7bb764

Please sign in to comment.