Skip to content
trampolining retries for clojure
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

Robert Bruce

Robert Bruce provides an easy way to execute a function and allow failures to be automatically retried. It's named after Robert the Bruce, whose determination was inspired by the sight a spider trying many time (and failing) to build a web.

Add this to your project.clj :dependencies list:

[robert/bruce "0.5.0"]


(use '[robert.bruce :only [try-try-again]])

;; arguments are like trampoline, if you want to default options
(try-try-again some-fn)
(try-try-again some-fn arg1 arg2)
(try-try-again #(some-fn arg1 arg2))

;; but with the addition of a first options arg, if you don't
(try-try-again {:sleep 5000 :tries 100} #(some-fn arg1 arg2))
(try-try-again {:sleep nil :tries 100} #(some-fn arg1 arg2))
(try-try-again {:sleep 5000 :tries 100} #(some-fn arg1 arg2))

(try-try-again {;; all options are optional

                ;; :sleep is used to specify how long to sleep
                ;; between retries, it can be a number, or false
                ;; or nil if you don't want to sleep, 
                ;; default is 10 seconds (that is, 10000)
                :sleep 100

                ;; :tries is used to specific how many tries
                ;; will be attempted, it can also be :unlimited
                ;; default is 5
                :tries 100

                ;; if you want to your sleep amount to change over
                ;; time, you can provide a decay function:
                ;; a number - your sleep will be multiplied by it
                ;; a function - your sleep will passed into it
                ;; a keyword - for out of the box decay algorithms
                ;;             :exponential, :double, :golden-ratio
                ;; default is identity
                :decay :exponential

                ;; if you want to only rety when particular
                ;; exceptions are thrown, you can add a :catch
                ;; clause.  it works with either a single type
                ;; or a collection.  
                ;; default is Exception
                :catch [ java.sql.SQLException]}
               #(some-fn arg1 arg2))


Copyright (C) 2011 Joe Gallo

Distributed under the Eclipse Public License, the same as Clojure.

Something went wrong with that request. Please try again.