summaryrefslogtreecommitdiff
path: root/lib/chef/http.rb
diff options
context:
space:
mode:
authorHui Hu <huh@vmware.com>2014-08-25 13:52:04 +0800
committerHui Hu <huh@vmware.com>2014-08-25 13:52:04 +0800
commitace6c1e64fa32efcb9beeb8238edb1d71dab659d (patch)
treeef741e12d56dbee00996969b0c8d44923e2720dd /lib/chef/http.rb
parent920d190cd817da50dcf87c3046b7c5f3bdcae7f9 (diff)
downloadchef-ace6c1e64fa32efcb9beeb8238edb1d71dab659d.tar.gz
retry on HTTP 50X Error when calling Chef REST API
Diffstat (limited to 'lib/chef/http.rb')
-rw-r--r--lib/chef/http.rb31
1 files changed, 19 insertions, 12 deletions
diff --git a/lib/chef/http.rb b/lib/chef/http.rb
index abc47f636e..bdfd30f140 100644
--- a/lib/chef/http.rb
+++ b/lib/chef/http.rb
@@ -289,11 +289,26 @@ class Chef
def retrying_http_errors(url)
http_attempts = 0
begin
- http_attempts += 1
-
- yield
-
+ loop do
+ http_attempts += 1
+ response, request, return_value = yield
+ # handle HTTP 50X Error
+ if response.kind_of?(Net::HTTPServerError)
+ if http_retry_count - http_attempts + 1 > 0
+ sleep_time = 1 + (2 ** http_attempts) + rand(2 ** http_attempts)
+ Chef::Log.error("Server returned error for #{url}, retrying #{http_attempts}/#{http_retry_count} in #{sleep_time}s")
+ sleep(sleep_time)
+ redo
+ end
+ end
+ return [response, request, return_value]
+ end
rescue SocketError, Errno::ETIMEDOUT => e
+ if http_retry_count - http_attempts + 1 > 0
+ Chef::Log.error("Error connecting to #{url}, retry #{http_attempts}/#{http_retry_count}")
+ sleep(http_retry_delay)
+ retry
+ end
e.message.replace "Error connecting to #{url} - #{e.message}"
raise e
rescue Errno::ECONNREFUSED
@@ -310,14 +325,6 @@ class Chef
retry
end
raise Timeout::Error, "Timeout connecting to #{url}, giving up"
- rescue Net::HTTPFatalError => e
- if http_retry_count - http_attempts + 1 > 0
- sleep_time = 1 + (2 ** http_attempts) + rand(2 ** http_attempts)
- Chef::Log.error("Server returned error for #{url}, retrying #{http_attempts}/#{http_retry_count} in #{sleep_time}s")
- sleep(sleep_time)
- retry
- end
- raise
end
end