-1

I would like to implement a retry for a number of times if the server is unavailable at that time!. Is possible?.

require 'httparty'
require 'pry'
require_relative '../../env.rb'

RSpec.describe 'Validar a api de usuários' do

  it 'Deve retornar 200 para API DRIVE THRU' do
    response = HttParty.get("#{URLS['drive_thru']}/v1/health/live")
    expect(response.code).to eql(200)
    expect(response.parsed_response).to be_nil
  end
end
  • It would be the same way you'd retry in regular code. E.g. a loop with a rescue or conditional. What is the error case you want to retry on? – max pleaner Nov 03 '20 at 18:47
  • Hello my friend, I honestly don't know how to do it. I want that if the api fails to run for 3X and if the server is unavailable it will present the message: "Server unavailable" – Silvio Aulik Nov 03 '20 at 19:08
  • But the specific details matter. What exactly happens right now when the server is unavailable? Do you get a SocketError raised? Or just a different response code? – max pleaner Nov 03 '20 at 21:34
  • Hi Max Morning!!.. fatal: unable to access 'https://gitlab.com/grupofleury/drive-thru/automacao-api.git/': The requested URL returned error: 503 – Silvio Aulik Nov 04 '20 at 12:06
  • Hi Max Morning!!.. fatal: unable to access 'gitlab.com/grupofleury/drive-thru/automacao-api.git': The requested URL returned error: 503 – Silvio Aulik 2 days ago Delete – Silvio Aulik Nov 06 '20 at 21:01

1 Answers1

0

A general purpose retry system might look like this:

tries_left = 3
loop do
  break if tries_left == 0
  begin
    result = do_thing
  rescue SomeError => e
    tries_left -= 1
  end
end
  

You have to replace do_thing and SomeError with your HTTParty call and whatever error is being raised.

Also see How can I handle errors with HTTParty?

max pleaner
  • 26,189
  • 9
  • 66
  • 118