Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defmacro retry-times (n timeout-secs &body body)
- (let ((i-sym (gensym "i"))
- (ex-sym (gensym "ex"))
- (loop-ex-sym (gensym "loop-ex"))
- (loop-tag-sym (gensym "loop-tag"))
- (timeout-secs (or timeout-secs 1)))
- `(loop
- named ,loop-tag-sym
- with ,loop-ex-sym = nil
- for ,i-sym below ,n
- do
- (handler-case
- (progn
- (setf ,loop-ex-sym nil)
- (return-from ,loop-tag-sym
- (progn ,@body)))
- (error (,ex-sym)
- (setf ,loop-ex-sym ,ex-sym)
- (format nil "failed with ~A retrying ~D/~D... ~%"
- ,ex-sym (1+ ,i-sym) ,n)
- (sleep ,timeout-secs)))
- while ,loop-ex-sym
- finally (error ,loop-ex-sym))))
Add Comment
Please, Sign In to add comment