Created
November 1, 2011 08:37
-
-
Save supairish/1330161 to your computer and use it in GitHub Desktop.
Retryable method
This file contains 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
# Options: | |
# * :tries - Number of retries to perform. Defaults to 1. | |
# * :on - The Exception on which a retry will be performed. Defaults to Exception, which retries on any Exception. | |
# | |
# Example | |
# ======= | |
# retryable(:tries => 1, :on => OpenURI::HTTPError) do | |
# # your code here | |
# end | |
# | |
def retryable(options = {}, &block) | |
opts = { :tries => 2, :on => Exception }.merge(options) | |
retry_exception, retries = opts[:on], opts[:tries] | |
begin | |
return yield | |
rescue retry_exception => e | |
Rails.logger.error "Error while trying to access Salesforce: #{e.to_s}" | |
Rails.logger.error e.class | |
Rails.logger.error e.backtrace.join("\n") | |
init_salesforce unless e.class == ActiveResource::ResourceNotFound | |
retry if (retries -= 1) > 0 | |
end | |
yield | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment