lib/ezid/requests/request.rb in ezid-client-1.9.4 vs lib/ezid/requests/request.rb in ezid-client-1.10.0
- old
+ new
@@ -20,10 +20,14 @@
GET = Net::HTTP::Get
PUT = Net::HTTP::Put
POST = Net::HTTP::Post
DELETE = Net::HTTP::Delete
+ RETRIABLE_SERVER_ERRORS = %w[500 502 503 504].freeze
+
+ RETRIES = ENV.fetch('EZID_REQUEST_RETRIES', '2').to_i
+
class << self
attr_accessor :http_method, :path, :response_class
def execute(client, *args)
request = new(client, *args)
@@ -35,11 +39,11 @@
name.split('::').last.sub('Request', '')
end
end
attr_reader :client
- def_delegators :client, :connection, :user, :password, :session
+ def_delegators :client, :connection, :user, :password, :session, :logger, :config
# @param client [Ezid::Client] the client
def initialize(client, *args)
@client = client
super build_request
@@ -48,16 +52,28 @@
# Executes the request and returns the response
# @return [Ezid::Response] the response
def execute
retries = 0
+
begin
- response_class.new(get_response_for_request)
- rescue Net::HTTPServerException, UnexpectedResponseError => e
- if retries < 2
- sleep client.config.retry_interval
+ http_response = get_response_for_request
+
+ if RETRIABLE_SERVER_ERRORS.include? http_response.code
+ raise ServerError, "#{http_response.code} #{http_response.msg}"
+ end
+
+ response_class.new(http_response)
+
+ rescue ServerError, UnexpectedResponseError => e
+ if retries < RETRIES
+ logger.error "EZID error: #{e}"
+
retries += 1
+ logger.info "Retry (#{retries} of #{RETRIES}) of #{short_name} #{path} in #{config.retry_interval} seconds ..."
+ sleep config.retry_interval
+
retry
else
raise
end
end
@@ -82,9 +98,13 @@
end
# HTTP request query string
# @return [String] the query string
def query; end
+
+ def short_name
+ self.class.short_name
+ end
def authentication_required?
true
end