Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force-trigger exception/retry #17

Closed
jacobmovingfwd opened this issue Sep 1, 2017 · 2 comments
Closed

Force-trigger exception/retry #17

jacobmovingfwd opened this issue Sep 1, 2017 · 2 comments

Comments

@jacobmovingfwd
Copy link

jacobmovingfwd commented Sep 1, 2017

Is there a way to force an exception that Retryable would pick up?
I'm trying to retry if HTTParty response.code is not a 200 or 204. I can get it to raise, but that goes to my begin/rescue, and skips out of Retryable's logic.

def httpPost(results, url = @url)
    #ensure_cb = Proc.new { |retries| puts "Total Retries: #{retries}" }
    exception_cb = Proc.new { |exception| puts "For URL: #{url}\nRetry-rescued error: #{exception.inspect}"}
    begin
      Retryable.retryable(:exception_cb => exception_cb, tries: 10, sleep: lambda { |n| 4**n }, on: [Errno::ECONNREFUSED, SocketError, Net::ReadTimeout, HTTParty::Error, Timeout::Error, Errno::EPIPE, OpenSSL::SSL::SSLError, EOFError]) do |retries, exception|
        response = HTTParty.post(url, body: results.to_json, headers: {'Content-Type' => 'application/json', 'User-Agent' => 'SendGrid 1.0'})
        puts "Attempt #{retries} failed with exception: #{exception}" if retries > 0
        exception if not [200, 204].include?(response.code) #throw http-code-level error if not a 200 or 204
        puts "Total Attempts: #{retries + 1}" if retries > 0
        return response
      end
    rescue StandardError => e
      puts "#{Time.now} - httpPost error: #{e.inspect}"
    end
  end
@nfedyashev
Copy link
Owner

@jacobmovingfwd

I may suggest to raise your own exception instead of the following line:

exception if not [200, 204].include?(response.code)

add it to the on: parameter and check exception type in :exception_cb proc.
Let me know if it works for you

@jacobmovingfwd
Copy link
Author

That did it perfectly! Thanks for the education.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants