Created
June 1, 2016 16:03
-
-
Save duduribeiro/ad553c0d448a00f8faa9e7a92f9c6e66 to your computer and use it in GitHub Desktop.
Retry on around filter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def retryable(options = {}) | |
opts = { :tries => 1, :on => Exception }.merge(options) | |
retry_exception, retries = opts[:on], opts[:tries] | |
begin | |
return yield | |
rescue retry_exception | |
if (retries -= 1) > 0 | |
sleep 2 | |
retry | |
else | |
raise | |
end | |
end | |
end | |
class ApplicationController < ActionController::Base | |
around_filter :retry_on_timeout | |
def retry_on_timeout | |
retryable(:tries => 10, :on => Timeout::Error) do | |
yield | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment