clojure重试库坚持不懈的使用

时间:2016-11-25 11:43:28

标签: clojure

尝试使用https://github.com/grammarly/perseverance, 为什么以下失败? IE浏览器。最后一次调用仍会产生异常

(defn fail-n
  "returns a function that will fail to process for the first n times is is called"
  [n]
  (let [cnt (atom 0)]
    (fn []
      (if (< @cnt n)
        (do (swap! cnt inc)
            (throw (RuntimeException. "Failed")))
        :success))))

(defn safe-fail-n [n]
  (p/retriable {
                ;;:catch [RuntimeException]
                } (fail-n n)))

;;(def f (fail-n 1))
(def f (safe-fail-n 2))

(p/retry {;;:strategy (p/constant-retry-strategy 0)
          ;;:catch [Exception]
          ;;:log-fn (fn [& a] (println "Retrying "a))
          }
          (f))

1 个答案:

答案 0 :(得分:1)

结帐github自述文件,您必须以p/retriable形式抓住RE

否则没有任何东西被抓住 - 或者重试

user=> (let [x (fail-n 5)] 
         (p/retry {} 
           (p/retriable {:catch [RuntimeException]} (x))))

java.lang.RuntimeException: Failed, retrying in 0,5 seconds...
java.lang.RuntimeException: Failed, retrying in 0,5 seconds...
java.lang.RuntimeException: Failed, retrying in 0,5 seconds...
java.lang.RuntimeException: Failed, retrying in 1,0 seconds...
java.lang.RuntimeException: Failed, retrying in 2,0 seconds...
:success

相关文件:

  

:catch - 应该是可重复捕获的异常类列表。 默认值为[ java.io.IOException ]。毅力不会故意捕获所有异常,以避免重试与IO无关的错误,这会绕过程序中正确的错误处理。 但是如果您确定内部的任何潜在异常都是可重复的,那么您可以随时提供:catch [Exception]