forked from sharetribe/dumpr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.clj
23 lines (22 loc) · 875 Bytes
/
utils.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
(ns dumpa.utils)
(defn retry
"Run the given function. If it throws, retry. Provide optional
parameters to control the retry logic."
([f] (retry f identity))
([f handler] (retry f handler (constantly true)))
([f handler should-retry?] (retry f handler should-retry? (* 120 1000)))
([f handler should-retry? max-wait] (retry f handler should-retry? max-wait 1000))
([f handler should-retry? max-wait start-wait]
(loop [wait start-wait]
(let [wait (min wait max-wait)
[success result] (try
[true (f)]
(catch Exception e
(handler e wait)
(Thread/sleep wait)
[false]))]
(if success
result
(do
(when (should-retry?)
(recur (* wait 2)))))))))