Class: Rightscale::SlicehostErrorHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/slicehost_base.rb

Overview

:nodoc:

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

Instance Method Summary collapse

Constructor Details

#initialize(slicehost, parser, params = {}) ⇒ SlicehostErrorHandler

params:

:reiteration_time
:errors_list
:close_on_error           = true | false
:close_on_4xx_probability = 1-100


364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/slicehost_base.rb', line 364

def initialize(slicehost, parser, params={}) #:nodoc:
  @slicehost     = slicehost
  @parser        = parser           # parser to parse a 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_probabilityObject



352
353
354
# File 'lib/slicehost_base.rb', line 352

def self.close_on_4xx_probability
  @@close_on_4xx_probability
end

.close_on_4xx_probability=(close_on_4xx_probability) ⇒ Object



355
356
357
# File 'lib/slicehost_base.rb', line 355

def self.close_on_4xx_probability=(close_on_4xx_probability)
  @@close_on_4xx_probability = close_on_4xx_probability
end

.close_on_errorObject



344
345
346
# File 'lib/slicehost_base.rb', line 344

def self.close_on_error
  @@close_on_error
end

.close_on_error=(close_on_error) ⇒ Object



347
348
349
# File 'lib/slicehost_base.rb', line 347

def self.close_on_error=(close_on_error)
  @@close_on_error = close_on_error
end

.reiteration_start_delayObject



328
329
330
# File 'lib/slicehost_base.rb', line 328

def self.reiteration_start_delay
  @@reiteration_start_delay
end

.reiteration_start_delay=(reiteration_start_delay) ⇒ Object



331
332
333
# File 'lib/slicehost_base.rb', line 331

def self.reiteration_start_delay=(reiteration_start_delay)
  @@reiteration_start_delay = reiteration_start_delay
end

.reiteration_timeObject



336
337
338
# File 'lib/slicehost_base.rb', line 336

def self.reiteration_time
  @@reiteration_time
end

.reiteration_time=(reiteration_time) ⇒ Object



339
340
341
# File 'lib/slicehost_base.rb', line 339

def self.reiteration_time=(reiteration_time)
  @@reiteration_time = reiteration_time
end

Instance Method Details

#check(request) ⇒ Object

Returns false if



378
379
380
381
382
383
384
385
386
387
388
389
390
391
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
# File 'lib/slicehost_base.rb', line 378

def check(request)  #:nodoc:
  result           = false
  error_found      = false
  error_match      = nil
  last_errors_text = ''
  response         = @slicehost.last_response
  # log error
  request_text_data = "#{request[:server]}:#{request[:port]}#{request[:request].path}"
  @slicehost.logger.warn("##### #{@slicehost.class.name} returned an error: #{response.code} #{response.message}\n#{response.body} #####")
  @slicehost.logger.warn("##### #{@slicehost.class.name} request: #{request_text_data} ####")

  if response.body && response.body[/<\?xml/]
    error_parser = SliceErrorResponseParser.new
    error_parser.parse(response)
    @slicehost.last_errors = error_parser.errors.map{|e| [response.code, e]}
    last_errors_text = @slicehost.last_errors.flatten.join("\n")
  else
    @slicehost.last_errors = [[response.code, "#{response.message} (#{request_text_data})"]]
    last_errors_text       = response.message
  end

  # now - check the error
  @errors_list.each do |error_to_find|
    if last_errors_text[/#{error_to_find}/i]
      error_found = true
      error_match = error_to_find
      @slicehost.logger.warn("##### Retry is needed, error pattern match: #{error_to_find} #####")
      break
    end
  end
    # check the time has gone from the first error come
  if 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 @close_on_error
      @slicehost.connection.finish "#{self.class.name}: error match to pattern '#{error_match}'"
    end

    if (Time.now < @stop_at)
      @retries += 1

      @slicehost.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
      result = @slicehost.request_info(request, @parser)
    else
      @slicehost.logger.warn("##### Ooops, time is over... ####")
    end
  # aha, this is unhandled error:
  elsif @close_on_error
    # Is this a 5xx error ?
    if @slicehost.last_response.code.to_s[/^5\d\d$/]
      @slicehost.connection.finish "#{self.class.name}: code: #{@slicehost.last_response.code}: '#{@slicehost.last_response.message}'"
    # Is this a 4xx error ?
    elsif @slicehost.last_response.code.to_s[/^4\d\d$/] && @close_on_4xx_probability > rand(100)
      @slicehost.connection.finish "#{self.class.name}: code: #{@slicehost.last_response.code}: '#{@slicehost.last_response.message}', " +
                             "probability: #{@close_on_4xx_probability}%"
    end
  end
  result
end