We have an application that makes hundreds of API calls to external services. Sometimes some calls take too much time to respond.
I am using the rake_timeout gem to find time consuming process, so, Timeout::Error
will be thrown whenever some request is taking too long to respond. I am rescuing this error and doing a retry on that method:
def new
@make_one_external_service_call = exteral_api_fetch1(params[:id])
@make_second_external_call = exteral_api_fetch1(@make_one_external_service_call)
#Below code will be repeated in every method
tries = 0
rescue Timeout::Error => e
tries += 1
retry if tries <= 3
logger.error e.message
end
This lets the method fully re-run it. This is very verbose and I am repeating it every time.
Is there any way to do this so that, if the Timeout:Error
occurrs, it will retry that method automatically three times?