Class: XRBP::WebClient::Plugins::AutoRetry

Inherits:
PluginBase
  • Object
show all
Defined in:
lib/xrbp/webclient/plugins/autoretry.rb

Overview

Plugin to automatically retry WebClient Connection requests multiple times.

If no max_tries are specified, requests will be tried indefinitely. Optionally configured interval to wait between retries.

Examples:

retrying request:

connection = WebClient::Connection.new
connection.add_plugin :autoretry

connection.max_tries = 3
connection.interval  = 1
connection.timeout   = 1

connection.url = "http://doesnt.exist"
connection.perform

Instance Attribute Summary collapse

Attributes inherited from PluginBase

#connection

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ AutoRetry

Returns a new instance of AutoRetry.



23
24
25
26
27
28
# File 'lib/xrbp/webclient/plugins/autoretry.rb', line 23

def initialize(connection)
  super(connection)
  @interval   = 3
  @max_tries  = nil
  @retry_num  = 0
end

Instance Attribute Details

#intervalObject

Returns the value of attribute interval.



21
22
23
# File 'lib/xrbp/webclient/plugins/autoretry.rb', line 21

def interval
  @interval
end

#max_triesObject

Returns the value of attribute max_tries.



21
22
23
# File 'lib/xrbp/webclient/plugins/autoretry.rb', line 21

def max_tries
  @max_tries
end

Instance Method Details

#addedObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/xrbp/webclient/plugins/autoretry.rb', line 30

def added
  plugin = self
  connection.define_instance_method(:retry_interval=) do |i|
    plugin.interval = i
  end

  connection.define_instance_method(:max_retries=) do |i|
    plugin.max_tries = i
  end
end

#handle_errorObject



41
42
43
44
45
46
47
48
# File 'lib/xrbp/webclient/plugins/autoretry.rb', line 41

def handle_error
  @retry_num += 1
  return nil if connection.force_quit? ||
                (!@max_tries.nil? && @retry_num > @max_tries)

  connection.rsleep(@interval)
  connection.perform
end