Class: RightAws::AWSErrorHandler
Constant Summary collapse
- DEFAULT_CLOSE_ON_4XX_PROBABILITY =
0-100 (%)
10
- @@reiteration_start_delay =
0.2
- @@reiteration_time =
5
- @@close_on_error =
true
- @@close_on_4xx_probability =
DEFAULT_CLOSE_ON_4XX_PROBABILITY
Class Method Summary collapse
- .close_on_4xx_probability ⇒ Object
- .close_on_4xx_probability=(close_on_4xx_probability) ⇒ Object
- .close_on_error ⇒ Object
- .close_on_error=(close_on_error) ⇒ Object
- .reiteration_start_delay ⇒ Object
- .reiteration_start_delay=(reiteration_start_delay) ⇒ Object
- .reiteration_time ⇒ Object
- .reiteration_time=(reiteration_time) ⇒ Object
Instance Method Summary collapse
-
#check(request) ⇒ Object
Returns false if.
-
#initialize(aws, parser, params = {}) ⇒ AWSErrorHandler
constructor
params: :reiteration_time :errors_list :close_on_error = true | false :close_on_4xx_probability = 1-100.
Constructor Details
#initialize(aws, parser, params = {}) ⇒ AWSErrorHandler
params:
:reiteration_time
:errors_list
:close_on_error = true | false
:close_on_4xx_probability = 1-100
378 379 380 381 382 383 384 385 386 387 388 389 |
# File 'lib/awsbase/right_awsbase.rb', line 378 def initialize(aws, parser, params={}) #:nodoc: @aws = aws # Link to RightEc2 | RightSqs | RightS3 instance @parser = parser # parser to parse Amazon response @started_at = Time.now @stop_at = @started_at + (params[:reiteration_time] || @@reiteration_time) @errors_list = params[:errors_list] || [] @reiteration_delay = @@reiteration_start_delay @retries = 0 # close current HTTP(S) connection on 5xx, errors from list and 4xx errors @close_on_error = params[:close_on_error].nil? ? @@close_on_error : params[:close_on_error] @close_on_4xx_probability = params[:close_on_4xx_probability] || @@close_on_4xx_probability end |
Class Method Details
.close_on_4xx_probability ⇒ Object
366 367 368 |
# File 'lib/awsbase/right_awsbase.rb', line 366 def self.close_on_4xx_probability @@close_on_4xx_probability end |
.close_on_4xx_probability=(close_on_4xx_probability) ⇒ Object
369 370 371 |
# File 'lib/awsbase/right_awsbase.rb', line 369 def self.close_on_4xx_probability=(close_on_4xx_probability) @@close_on_4xx_probability = close_on_4xx_probability end |
.close_on_error ⇒ Object
358 359 360 |
# File 'lib/awsbase/right_awsbase.rb', line 358 def self.close_on_error @@close_on_error end |
.close_on_error=(close_on_error) ⇒ Object
361 362 363 |
# File 'lib/awsbase/right_awsbase.rb', line 361 def self.close_on_error=(close_on_error) @@close_on_error = close_on_error end |
.reiteration_start_delay ⇒ Object
342 343 344 |
# File 'lib/awsbase/right_awsbase.rb', line 342 def self.reiteration_start_delay @@reiteration_start_delay end |
.reiteration_start_delay=(reiteration_start_delay) ⇒ Object
345 346 347 |
# File 'lib/awsbase/right_awsbase.rb', line 345 def self.reiteration_start_delay=(reiteration_start_delay) @@reiteration_start_delay = reiteration_start_delay end |
.reiteration_time ⇒ Object
350 351 352 |
# File 'lib/awsbase/right_awsbase.rb', line 350 def self.reiteration_time @@reiteration_time end |
.reiteration_time=(reiteration_time) ⇒ Object
353 354 355 |
# File 'lib/awsbase/right_awsbase.rb', line 353 def self.reiteration_time=(reiteration_time) @@reiteration_time = reiteration_time end |
Instance Method Details
#check(request) ⇒ Object
Returns false if
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 |
# File 'lib/awsbase/right_awsbase.rb', line 392 def check(request) #:nodoc: result = false error_found = false redirect_detected= false error_match = nil last_errors_text = '' response = @aws.last_response # log error request_text_data = "#{request[:server]}:#{request[:port]}#{request[:request].path}" # is this a redirect? # yes! if response.is_a?(Net::HTTPRedirection) redirect_detected = true else # no, it's an error ... @aws.logger.warn("##### #{@aws.class.name} returned an error: #{response.code} #{response.}\n#{response.body} #####") @aws.logger.warn("##### #{@aws.class.name} request: #{request_text_data} ####") end # Check response body: if it is an Amazon XML document or not: if redirect_detected || (response.body && response.body[/<\?xml/]) # ... it is a xml document @aws.class.bench_xml.add! do error_parser = RightErrorResponseParser.new error_parser.parse(response) @aws.last_errors = error_parser.errors @aws.last_request_id = error_parser.requestID last_errors_text = @aws.last_errors.flatten.join("\n") # on redirect : if redirect_detected location = response['location'] # ... log information and ... @aws.logger.info("##### #{@aws.class.name} redirect requested: #{response.code} #{response.} #####") @aws.logger.info("##### New location: #{location} #####") # ... fix the connection data request[:server] = URI.parse(location).host request[:protocol] = URI.parse(location).scheme request[:port] = URI.parse(location).port end end else # ... it is not a xml document(probably just a html page?) @aws.last_errors = [[response.code, "#{response.} (#{request_text_data})"]] @aws.last_request_id = '-undefined-' last_errors_text = response. end # now - check the error unless redirect_detected @errors_list.each do |error_to_find| if last_errors_text[/#{error_to_find}/i] error_found = true error_match = error_to_find @aws.logger.warn("##### Retry is needed, error pattern match: #{error_to_find} #####") break end end end # check the time has gone from the first error come if redirect_detected || error_found # Close the connection to the server and recreate a new one. # It may have a chance that one server is a semi-down and reconnection # will help us to connect to the other server if !redirect_detected && @close_on_error @aws.connection.finish "#{self.class.name}: error match to pattern '#{error_match}'" end if (Time.now < @stop_at) @retries += 1 unless redirect_detected @aws.logger.warn("##### Retry ##{@retries} is being performed. Sleeping for #{@reiteration_delay} sec. Whole time: #{Time.now-@started_at} sec ####") sleep @reiteration_delay @reiteration_delay *= 2 # Always make sure that the fp is set to point to the beginning(?) # of the File/IO. TODO: it assumes that offset is 0, which is bad. if(request[:request].body_stream && request[:request].body_stream.respond_to?(:pos)) begin request[:request].body_stream.pos = 0 rescue Exception => e @logger.warn("Retry may fail due to unable to reset the file pointer" + " -- #{self.class.name} : #{e.inspect}") end end else @aws.logger.info("##### Retry ##{@retries} is being performed due to a redirect. ####") end result = @aws.request_info(request, @parser) else @aws.logger.warn("##### Ooops, time is over... ####") end # aha, this is unhandled error: elsif @close_on_error # Is this a 5xx error ? if @aws.last_response.code.to_s[/^5\d\d$/] @aws.connection.finish "#{self.class.name}: code: #{@aws.last_response.code}: '#{@aws.last_response.}'" # Is this a 4xx error ? elsif @aws.last_response.code.to_s[/^4\d\d$/] && @close_on_4xx_probability > rand(100) @aws.connection.finish "#{self.class.name}: code: #{@aws.last_response.code}: '#{@aws.last_response.}', " + "probability: #{@close_on_4xx_probability}%" end end result end |