mirror of
https://github.com/jimeh/amqp-failover.git
synced 2026-02-19 10:56:44 +00:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5c1f55dd66 | |||
| 561a554b3f | |||
| c9f389ce84 |
@@ -29,13 +29,13 @@ module AMQP
|
|||||||
# - :selection, not yet implimented.
|
# - :selection, not yet implimented.
|
||||||
#
|
#
|
||||||
def connect_with_failover(opts = nil)
|
def connect_with_failover(opts = nil)
|
||||||
opts = parse_amqp_url_or_opts(opts)
|
opts = parse_amqp_url_or_opts_with_failover(opts)
|
||||||
connect_without_failover(opts)
|
connect_without_failover(opts)
|
||||||
end
|
end
|
||||||
alias :connect_without_failover :connect
|
alias :connect_without_failover :connect
|
||||||
alias :connect :connect_with_failover
|
alias :connect :connect_with_failover
|
||||||
|
|
||||||
def parse_amqp_url_or_opts(opts = nil)
|
def parse_amqp_url_or_opts_with_failover(opts = nil)
|
||||||
if opts.is_a?(String) && opts.index(',').nil?
|
if opts.is_a?(String) && opts.index(',').nil?
|
||||||
opts = init_failover(opts.split(','))
|
opts = init_failover(opts.split(','))
|
||||||
elsif opts.is_a?(Array)
|
elsif opts.is_a?(Array)
|
||||||
@@ -56,12 +56,19 @@ module AMQP
|
|||||||
|
|
||||||
end # << self
|
end # << self
|
||||||
|
|
||||||
def disconnected_with_failover
|
def initialize_with_failover(opts = {})
|
||||||
return failover_switch if @failover
|
@failover = opts.delete(:failover) if opts.has_key?(:failover)
|
||||||
disconnected_without_failover
|
initialize_without_failover(opts)
|
||||||
end
|
end
|
||||||
alias :disconnected_without_failover :disconnected
|
alias :initialize_without_failover :initialize
|
||||||
alias :disconnected :disconnected_with_failover
|
alias :initialize :initialize_with_failover
|
||||||
|
|
||||||
|
def unbind_with_failover
|
||||||
|
@on_disconnect = method(:failover_switch) if @failover
|
||||||
|
unbind_without_failover
|
||||||
|
end
|
||||||
|
alias :unbind_without_failover :unbind
|
||||||
|
alias :unbind :unbind_with_failover
|
||||||
|
|
||||||
end # Client
|
end # Client
|
||||||
end # AMQP
|
end # AMQP
|
||||||
@@ -10,12 +10,6 @@ module AMQP
|
|||||||
attr_accessor :settings
|
attr_accessor :settings
|
||||||
attr_accessor :on_disconnect
|
attr_accessor :on_disconnect
|
||||||
|
|
||||||
def self.extended(base)
|
|
||||||
if (base.failover = base.settings.delete(:failover))
|
|
||||||
base.on_disconnect = base.method(:disconnected)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def failover_switch
|
def failover_switch
|
||||||
if (new_settings = @failover.from(@settings))
|
if (new_settings = @failover.from(@settings))
|
||||||
log_message = "Could not connect to or lost connection to server #{@settings[:host]}:#{@settings[:port]}. " +
|
log_message = "Could not connect to or lost connection to server #{@settings[:host]}:#{@settings[:port]}. " +
|
||||||
@@ -26,7 +20,7 @@ module AMQP
|
|||||||
if @failover.options[:fallback] && @failover.primary == @settings
|
if @failover.options[:fallback] && @failover.primary == @settings
|
||||||
fallback(@failover.primary, @failover.fallback_interval)
|
fallback(@failover.primary, @failover.fallback_interval)
|
||||||
end
|
end
|
||||||
@settings = new_settings
|
@settings = new_settings.merge({:failover => @failover})
|
||||||
reconnect
|
reconnect
|
||||||
else
|
else
|
||||||
raise Error, "Could not connect to server #{@settings[:host]}:#{@settings[:port]}"
|
raise Error, "Could not connect to server #{@settings[:host]}:#{@settings[:port]}"
|
||||||
|
|||||||
Reference in New Issue
Block a user