summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorTimothee Cour <timothee.cour2@gmail.com>2020-06-02 01:47:19 -0700
committerGitHub <noreply@github.com>2020-06-02 10:47:19 +0200
commit621384b8efce7def08faeb9bc63289a4a88c3a59 (patch)
tree06d9d62478d651dac8f20164c9646ca836cb6441 /lib
parente5b64af8317eb0f5e8b9912691b69aab4cd26adf (diff)
downloadNim-621384b8efce7def08faeb9bc63289a4a88c3a59.tar.gz
fix https://github.com/timotheecour/Nim/issues/266 retry on failure to avoid common 503 github errors (#14547)
Diffstat (limited to 'lib')
-rw-r--r--lib/std/private/nimbleutils.nim32
1 files changed, 32 insertions, 0 deletions
diff --git a/lib/std/private/nimbleutils.nim b/lib/std/private/nimbleutils.nim
new file mode 100644
index 000000000..297fb9695
--- /dev/null
+++ b/lib/std/private/nimbleutils.nim
@@ -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