Module: AMQP::FailoverClient

Includes:
BasicClient
Defined in:
lib/amqp/failover_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#failoverObject

Returns the value of attribute failover.



7
8
9
# File 'lib/amqp/failover_client.rb', line 7

def failover
  @failover
end

#fallback_callbackObject



51
52
53
54
55
56
57
# File 'lib/amqp/failover_client.rb', line 51

def fallback_callback
  #TODO: Figure out a way to artificially trigger EM to disconnect on fallback without channels being closed.
  @fallback_callback ||= proc { |conf, retry_interval|
    clean_exit("Primary server (#{conf[:host]}:#{conf[:port]}) is back. " +
               "Performing clean exit to be relaunched with primary config.")
  }
end

#fallback_monitorObject (readonly)

Returns the value of attribute fallback_monitor.



8
9
10
# File 'lib/amqp/failover_client.rb', line 8

def fallback_monitor
  @fallback_monitor
end

#on_disconnectObject

Returns the value of attribute on_disconnect.



11
12
13
# File 'lib/amqp/failover_client.rb', line 11

def on_disconnect
  @on_disconnect
end

#settingsObject

Returns the value of attribute settings.



10
11
12
# File 'lib/amqp/failover_client.rb', line 10

def settings
  @settings
end

Instance Method Details

#clean_exit(msg = nil) ⇒ Object



38
39
40
41
42
43
# File 'lib/amqp/failover_client.rb', line 38

def clean_exit(msg = nil)
  msg ||= "clean exit"
  logger.info(msg)
  logger.error(msg)
  Process.exit
end

#configsObject



34
35
36
# File 'lib/amqp/failover_client.rb', line 34

def configs
  @failover.configs if @failover
end

#failover_switchObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/amqp/failover_client.rb', line 13

def failover_switch
  if (new_settings = @failover.from(@settings))
    log_message = "Could not connect to or lost connection to server #{@settings[:host]}:#{@settings[:port]}. " +
                  "Attempting connection to: #{new_settings[:host]}:#{new_settings[:port]}"
    logger.error(log_message)
    logger.info(log_message)
    
    if @failover.options[:fallback] && @failover.primary == @settings
      fallback(@failover.primary, @failover.fallback_interval)
    end
    @settings = new_settings.merge({:failover => @failover})
    reconnect
  else
    raise Error, "Could not connect to server #{@settings[:host]}:#{@settings[:port]}"
  end
end

#fallback(conf = {}, retry_interval = nil) ⇒ Object



45
46
47
48
49
# File 'lib/amqp/failover_client.rb', line 45

def fallback(conf = {}, retry_interval = nil)
  @fallback_monitor = Failover::ServerDiscovery.monitor(conf, retry_interval) do
    fallback_callback.call(conf, retry_interval)
  end
end

#loggerObject



30
31
32
# File 'lib/amqp/failover_client.rb', line 30

def logger
  Failover.logger
end