mirror of
https://github.com/jimeh/amqp-failover.git
synced 2026-02-19 10:56:44 +00:00
Failover::Configs specs pass
This commit is contained in:
@@ -20,7 +20,7 @@ module AMQP
|
|||||||
attr_accessor :fallback
|
attr_accessor :fallback
|
||||||
|
|
||||||
def initialize(confs = nil, opts = {})
|
def initialize(confs = nil, opts = {})
|
||||||
@configs = Configs.new(confs)
|
@configs = Failover::Configs.new(confs)
|
||||||
@options = default_options.merge(opts)
|
@options = default_options.merge(opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -32,7 +32,7 @@ module AMQP
|
|||||||
def default_options
|
def default_options
|
||||||
{ :retry_timeout => 1,
|
{ :retry_timeout => 1,
|
||||||
:selection => :sequential, #TODO: Impliment next server selection algorithm
|
:selection => :sequential, #TODO: Impliment next server selection algorithm
|
||||||
:fallback => false,
|
:fallback => false, #TODO: Enable by default once a sane solution is found
|
||||||
:fallback_interval => 10 }
|
:fallback_interval => 10 }
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -56,17 +56,6 @@ module AMQP
|
|||||||
@configs ||= Config.new
|
@configs ||= Config.new
|
||||||
end
|
end
|
||||||
|
|
||||||
def configs=(confs = [])
|
|
||||||
@configs = nil
|
|
||||||
confs.each do |conf|
|
|
||||||
if conf.is_a?(Array)
|
|
||||||
add_config(conf[1], conf[0])
|
|
||||||
else
|
|
||||||
add_config(conf)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def add_config(conf = {}, ref = nil)
|
def add_config(conf = {}, ref = nil)
|
||||||
index = configs.index(conf)
|
index = configs.index(conf)
|
||||||
configs << Config::Failed.new(conf) if index.nil?
|
configs << Config::Failed.new(conf) if index.nil?
|
||||||
|
|||||||
@@ -7,10 +7,14 @@ module AMQP
|
|||||||
attr_accessor :last_fail
|
attr_accessor :last_fail
|
||||||
|
|
||||||
def initialize(hash = {}, last_fail_date = nil)
|
def initialize(hash = {}, last_fail_date = nil)
|
||||||
self.replace(symbolize_keys(hash))
|
self.replace(symbolize_keys(defaults.merge(hash)))
|
||||||
self.last_fail = last_fail_date if last_fail_date
|
self.last_fail = last_fail_date if last_fail_date
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def defaults
|
||||||
|
AMQP.settings
|
||||||
|
end
|
||||||
|
|
||||||
def symbolize_keys(hash = {})
|
def symbolize_keys(hash = {})
|
||||||
hash.inject({}) do |result, (key, value)|
|
hash.inject({}) do |result, (key, value)|
|
||||||
result[key.is_a?(String) ? key.to_sym : key] = value
|
result[key.is_a?(String) ? key.to_sym : key] = value
|
||||||
@@ -30,7 +34,7 @@ module AMQP
|
|||||||
end
|
end
|
||||||
return other.last_fail <=> self.last_fail
|
return other.last_fail <=> self.last_fail
|
||||||
end
|
end
|
||||||
return 0
|
super(other)
|
||||||
end
|
end
|
||||||
|
|
||||||
end # Config
|
end # Config
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ module AMQP
|
|||||||
end
|
end
|
||||||
|
|
||||||
def set(conf = {}, ref = nil)
|
def set(conf = {}, ref = nil)
|
||||||
conf = Config.new(default_config.merge(conf))
|
conf = Failover::Config.new(conf) if !conf.is_a?(Failover::Config)
|
||||||
self << conf if (index = self.index(conf)).nil?
|
self << conf if (index = self.index(conf)).nil?
|
||||||
if ref
|
if ref
|
||||||
refs[ref] = (index || self.index(conf))
|
refs[ref] = (index || self.index(conf))
|
||||||
@@ -75,7 +75,7 @@ module AMQP
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.load_array(confs = [])
|
def load_array(confs = [])
|
||||||
self.clear
|
self.clear
|
||||||
confs.each do |conf|
|
confs.each do |conf|
|
||||||
conf = AMQP::Client.parse_amqp_url(conf) if conf.is_a?(::String)
|
conf = AMQP::Client.parse_amqp_url(conf) if conf.is_a?(::String)
|
||||||
@@ -84,11 +84,7 @@ module AMQP
|
|||||||
end
|
end
|
||||||
|
|
||||||
def load_hash(conf = {})
|
def load_hash(conf = {})
|
||||||
set(Config.new(conf))
|
set(conf)
|
||||||
end
|
|
||||||
|
|
||||||
def default_config
|
|
||||||
AMQP.settings
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end # Config
|
end # Config
|
||||||
|
|||||||
@@ -38,7 +38,9 @@ module AMQP
|
|||||||
logger.error(log_message)
|
logger.error(log_message)
|
||||||
logger.info(log_message)
|
logger.info(log_message)
|
||||||
|
|
||||||
fallback(@failover.primary, @failover.fallback_interval) if @failover.primary == @settings
|
if @failover.options[:fallback] && @failover.primary == @settings
|
||||||
|
fallback(@failover.primary, @failover.fallback_interval)
|
||||||
|
end
|
||||||
@settings = new_settings
|
@settings = new_settings
|
||||||
reconnect
|
reconnect
|
||||||
else
|
else
|
||||||
@@ -53,6 +55,7 @@ module AMQP
|
|||||||
end
|
end
|
||||||
|
|
||||||
def fallback_callback
|
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|
|
@fallback_callback ||= proc { |conf, retry_interval|
|
||||||
clean_exit("Primary server (#{conf[:host]}:#{conf[:port]}) is back. " +
|
clean_exit("Primary server (#{conf[:host]}:#{conf[:port]}) is back. " +
|
||||||
"Performing clean exit to be relaunched with primary config.")
|
"Performing clean exit to be relaunched with primary config.")
|
||||||
|
|||||||
@@ -3,52 +3,39 @@ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__))
|
|||||||
|
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
describe 'AMQP::Failover::Config' do
|
describe 'AMQP::Failover::Configs' do
|
||||||
|
|
||||||
before(:all) do
|
|
||||||
# @conf = AMQP::Failover::Config.new
|
|
||||||
end
|
|
||||||
|
|
||||||
before(:each) do
|
before(:each) do
|
||||||
@conf = AMQP::Failover::Config.new
|
@conf = AMQP::Failover::Configs.new
|
||||||
# [:primary, :configs, :refs].each do |var|
|
|
||||||
# @conf.instance_variable_set("@#{var}", nil)
|
|
||||||
# end
|
|
||||||
@raw_configs = [
|
@raw_configs = [
|
||||||
{:host => 'rabbit3.local'},
|
{:host => 'rabbit3.local'},
|
||||||
{:host => 'rabbit2.local'},
|
{:host => 'rabbit2.local'},
|
||||||
{:host => 'rabbit2.local', :port => 5673}
|
{:host => 'rabbit2.local', :port => 5673}
|
||||||
]
|
]
|
||||||
@configs = @raw_configs.map { |conf| @conf.default_config.merge(conf) }
|
@configs = @raw_configs.map { |conf| AMQP.settings.merge(conf) }
|
||||||
end
|
|
||||||
|
|
||||||
after(:each) do
|
|
||||||
# [:primary, :configs, :refs].each do |var|
|
|
||||||
# @conf.instance_variable_set("@#{var}", nil)
|
|
||||||
# end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should set and get configs" do
|
it "should set and get configs" do
|
||||||
@conf.primary.should == 0
|
@conf.primary.should == 0
|
||||||
@conf.configs.should have(0).items
|
@conf.should have(0).items
|
||||||
|
|
||||||
@conf.set(@raw_configs[0])
|
@conf.set(@raw_configs[0])
|
||||||
@conf.configs.should have(1).items
|
@conf.should have(1).items
|
||||||
@conf.get(0).should == @configs[0]
|
@conf.get(0).should == @configs[0]
|
||||||
@conf[0].should == @configs[0]
|
@conf[0].should == @configs[0]
|
||||||
|
|
||||||
@conf.set(@raw_configs[1])
|
@conf.set(@raw_configs[1])
|
||||||
@conf.configs.should have(2).items
|
@conf.should have(2).items
|
||||||
@conf.get(1).should == @configs[1]
|
@conf.get(1).should == @configs[1]
|
||||||
@conf[1].should == @configs[1]
|
@conf[1].should == @configs[1]
|
||||||
|
|
||||||
@conf.set(@raw_configs[1], :the_one)
|
@conf.set(@raw_configs[1], :the_one)
|
||||||
@conf.configs.should have(2).items
|
@conf.should have(2).items
|
||||||
@conf.get(1).should == @configs[1]
|
@conf.get(1).should == @configs[1]
|
||||||
@conf[:the_one].should == @configs[1]
|
@conf[:the_one].should == @configs[1]
|
||||||
|
|
||||||
@conf.load_array(@raw_configs)
|
@conf.load_array(@raw_configs)
|
||||||
@conf.configs.should have(3).items
|
@conf.should have(3).items
|
||||||
@conf.get_primary.should == @configs[0]
|
@conf.get_primary.should == @configs[0]
|
||||||
@conf.primary = 1
|
@conf.primary = 1
|
||||||
@conf.get_primary.should == @configs[1]
|
@conf.get_primary.should == @configs[1]
|
||||||
@@ -57,7 +44,7 @@ describe 'AMQP::Failover::Config' do
|
|||||||
|
|
||||||
it "should #find_next" do
|
it "should #find_next" do
|
||||||
@conf.load(@raw_configs)
|
@conf.load(@raw_configs)
|
||||||
@conf.configs.should have(3).items
|
@conf.should have(3).items
|
||||||
@conf.find_next(@configs[0]).should == @configs[1]
|
@conf.find_next(@configs[0]).should == @configs[1]
|
||||||
@conf.find_next(@configs[1]).should == @configs[2]
|
@conf.find_next(@configs[1]).should == @configs[2]
|
||||||
@conf.find_next(@configs[2]).should == @configs[0]
|
@conf.find_next(@configs[2]).should == @configs[0]
|
||||||
Reference in New Issue
Block a user