Force-trigger exception/retry
Closed this issue · 2 comments
jacobmovingfwd commented
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 commented
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 commented
That did it perfectly! Thanks for the education.