Skip to content

Instantly share code, notes, and snippets.

@supairish
Created November 1, 2011 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save supairish/1330161 to your computer and use it in GitHub Desktop.
Save supairish/1330161 to your computer and use it in GitHub Desktop.
Retryable method
# 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