Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

better retry handling

  • Loading branch information...
commit bfd0120e3020cc0fb5b94dcb3c0dd47bfb1fa0c4 1 parent 364cbcc
@chicks chicks authored
View
64 lib/sugarcrm/connection/connection.rb
@@ -14,6 +14,7 @@ module SugarCRM; class Connection
attr :options, true
attr :request, true
attr :response, true
+ attr :errors, true
# This is the singleton connection class.
def initialize(url, user, pass, options={})
@@ -22,6 +23,7 @@ def initialize(url, user, pass, options={})
:register_modules => true,
:load_environment => true
}.merge(options)
+ @errors = []
@url = URI.parse(url)
@user = user
@pass = pass
@@ -68,8 +70,10 @@ def connect!
alias :reconnect! :connect!
# Send a request to the Sugar Instance
- def send!(method, json)
- nb_failed_attempts = 0 # how many times we have failed to send
+ def send!(method, json, max_retry=3)
+ if max_retry == 0
+ raise SugarCRM::RetryLimitExceeded, "SugarCRM::Connection Errors: \n#{@errors.reverse.join "\n\s\s"}"
+ end
@request = SugarCRM::Request.new(@url, method, json, @options[:debug])
# Send Ze Request
begin
@@ -80,40 +84,25 @@ def send!(method, json)
end
return handle_response
# Timeouts are usually a server side issue
- rescue Timeout::Error
- nb_failed_attempts += 1
- unless nb_failed_attempts >= 3
- retry
- else
- raise
- end
+ rescue Timeout::Error => error
+ @errors << error
+ send!(method, json, max_retry.pred)
# Lower level errors requiring a reconnect
- rescue Errno::ECONNRESET, Errno::ECONNABORTED, Errno::EPIPE, EOFError
- nb_failed_attempts += 1
- unless nb_failed_attempts >= 3
- retry!(method, json)
- else
- raise
- end
+ rescue Errno::ECONNRESET, Errno::ECONNABORTED, Errno::EPIPE, EOFError => error
+ @errors << error
+ reconnect!
+ send!(method, json, max_retry.pred)
# Handle invalid sessions
- rescue SugarCRM::InvalidSession
- nb_failed_attempts += 1
- unless nb_failed_attempts >= 3
- login!
- retry
- else
- raise
- end
- end
- end
-
- # Sometimes our connection just disappears but we still have a session.
- # This method forces a reconnect and relogin to update the session and resend
- # the request.
- def retry!(method, json)
- reconnect!
- send!(method,json)
+ rescue SugarCRM::InvalidSession => error
+ @errors << error
+ old_session = @sugar_session_id.dup
+ login!
+ # Update the session id in the request that we want to retry.
+ json.gsub!(old_session, @sugar_session_id)
+ send!(method, json, max_retry.pred)
+ end
end
+ alias :retry! :send!
def debug=(debug)
options[:debug] = debug
@@ -180,13 +169,14 @@ def parse_response
# Complain if we can't parse
raise UnhandledResponse, @response.body
end
+ # Do ze debugs!
+ nice_debugging_for json
# Check for an invalid session
invalid_session? json
# Check for an empty result set
if zero_results? json
return nil
end
- nice_debugging_for json
json
end
@@ -196,9 +186,9 @@ def parse_response
# "number"=>11,
# "description"=>"The session ID is invalid"}
def invalid_session?(json)
- if json["name"]
- raise SugarCRM::InvalidSession if json["name"] == "Invalid Session ID"
- end
+ return false unless json["name"]
+ return false if @request.method == :logout
+ raise SugarCRM::InvalidSession if json["name"] == "Invalid Session ID"
end
def zero_results?(json)
View
1  lib/sugarcrm/exceptions.rb
@@ -2,6 +2,7 @@ module SugarCRM
class NoActiveSession < RuntimeError; end
class MultipleSessions < RuntimeError; end
class InvalidSession < RuntimeError; end
+ class RetryLimitExceeded < RuntimeError; end
class LoginError < RuntimeError; end
class MissingCredentials < RuntimeError; end
class ConnectionTimeoutError < RuntimeError; end
View
4 lib/sugarcrm/session.rb
@@ -111,9 +111,7 @@ def load_config(path, opts = {})
end
def update_config(params)
- params.each{|k,v|
- @config[k.to_sym] = v
- }
+ params.each{ |k,v| @config[k.to_sym] = v }
@config
end
View
3  test/test_session.rb
@@ -15,7 +15,6 @@
class TestSession < ActiveSupport::TestCase
context "The SugarCRM::Session class" do
should "raise SugarCRM::MissingCredentials if at least one of url/username/password is missing" do
-
assert_raise(SugarCRM::MissingCredentials){ SugarCRM.connect('http://127.0.0.1/sugarcrm', nil, nil) }
end
@@ -32,7 +31,7 @@ class TestSession < ActiveSupport::TestCase
Three.disconnect!
end
end
-
+
should "parse config parameters from a file" do
assert_equal CONFIG_CONTENTS, SugarCRM::Session.parse_config_file(CONFIG_TEST_PATH)
end
Please sign in to comment.
Something went wrong with that request. Please try again.