Class: AMQP::Failover

Inherits:
Object
  • Object
show all
Defined in:
lib/amqp/failover.rb,
lib/amqp/failover/config.rb,
lib/amqp/failover/logger.rb,
lib/amqp/failover/version.rb,
lib/amqp/failover/configurations.rb,
lib/amqp/failover/server_discovery.rb

Defined Under Namespace

Classes: Config, Configurations, Logger, ServerDiscovery

Constant Summary collapse

VERSION =
"0.0.3"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(confs = nil, opts = {}) ⇒ Failover

Returns a new instance of Failover.



20
21
22
23
24
# File 'lib/amqp/failover.rb', line 20

def initialize(confs = nil, opts = {})
  @configs = Failover::Configurations.new(confs)
  @options = default_options.merge(opts)
  @configs.primary_ref = @options[:primary_config]
end

Class Attribute Details

.loggerObject

pluggable logger specifically for tracking failover and fallbacks



28
29
30
# File 'lib/amqp/failover.rb', line 28

def logger
  @logger ||= Logger.new
end

Instance Attribute Details

#fallbackObject

Returns the value of attribute fallback.



18
19
20
# File 'lib/amqp/failover.rb', line 18

def fallback
  @fallback
end

#latest_failedObject (readonly)

Returns the value of attribute latest_failed.



15
16
17
# File 'lib/amqp/failover.rb', line 15

def latest_failed
  @latest_failed
end

#primaryObject

Returns the value of attribute primary.



16
17
18
# File 'lib/amqp/failover.rb', line 16

def primary
  @primary
end

#retry_timeoutObject

Returns the value of attribute retry_timeout.



17
18
19
# File 'lib/amqp/failover.rb', line 17

def retry_timeout
  @retry_timeout
end

Instance Method Details

#add_config(conf = {}, ref = nil) ⇒ Object



62
63
64
65
66
# File 'lib/amqp/failover.rb', line 62

def add_config(conf = {}, ref = nil)
  index = configs.index(conf)
  configs.set(conf) if index.nil?
  refs[ref] = (index || configs.index(conf)) if !ref.nil?
end

#configsObject



58
59
60
# File 'lib/amqp/failover.rb', line 58

def configs
  @configs ||= Configurations.new
end

#default_optionsObject



34
35
36
37
38
39
40
# File 'lib/amqp/failover.rb', line 34

def default_options
  { :primary_config => 0,
    :retry_timeout => 1,
    :selection => :sequential, #TODO: Implement next server selection algorithm
    :fallback => false, #TODO: Enable by default once a sane implementation is figured out
    :fallback_interval => 10 }
end

#failed_with(conf = {}, ref = nil, time = nil) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/amqp/failover.rb', line 74

def failed_with(conf = {}, ref = nil, time = nil)
  time ||= Time.now
  if !(index = configs.index(conf)).nil?
    configs[index].last_fail = time
    @latest_failed = configs[index]
  else
    @latest_failed = configs.set(conf)
    configs.last.last_fail = time
  end
  refs[ref] = (index || configs.index(conf)) if !ref.nil?
end

#failover_from(conf = {}, time = nil) ⇒ Object Also known as: from



68
69
70
71
# File 'lib/amqp/failover.rb', line 68

def failover_from(conf = {}, time = nil)
  failed_with(conf, nil, time)
  next_config
end

#fallback_intervalObject



46
47
48
# File 'lib/amqp/failover.rb', line 46

def fallback_interval
  options[:fallback_interval] ||= default_options[:fallback_interval]
end

#get_by_conf(conf = {}) ⇒ Object



102
103
104
# File 'lib/amqp/failover.rb', line 102

def get_by_conf(conf = {})
  configs[configs.index(conf)]
end

#get_by_ref(ref = nil) ⇒ Object



106
107
108
# File 'lib/amqp/failover.rb', line 106

def get_by_ref(ref = nil)
  configs[refs[ref]] if refs[ref]
end

#last_fail_of(match) ⇒ Object



98
99
100
# File 'lib/amqp/failover.rb', line 98

def last_fail_of(match)
  ((match.is_a?(Hash) ? get_by_conf(match) : get_by_ref(match)) || Config::Failed.new).last_fail
end

#next_config(retry_timeout = nil, after = nil) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/amqp/failover.rb', line 86

def next_config(retry_timeout = nil, after = nil)
  return nil if configs.size <= 1
  retry_timeout ||= @options[:retry_timeout]
  after ||= @latest_failed
  index = configs.index(after)
  available = (index > 0) ? configs[index+1..-1] + configs[0..index-1] : configs[1..-1]
  available.each do |conf|
    return conf if conf.last_fail.nil? || (conf.last_fail.to_i + retry_timeout) < Time.now.to_i
  end
  return nil
end

#optionsObject



42
43
44
# File 'lib/amqp/failover.rb', line 42

def options
  @options ||= {}
end

#refsObject



54
55
56
# File 'lib/amqp/failover.rb', line 54

def refs
  @refs ||= {}
end