mirror of
https://github.com/jimeh/amqp-failover.git
synced 2026-02-19 10:56:44 +00:00
restructured a few things again
This commit is contained in:
@@ -4,10 +4,11 @@ require 'yaml'
|
||||
|
||||
require 'amqp/failover_client'
|
||||
require 'amqp/failover/config'
|
||||
require 'amqp/failover/fallback'
|
||||
require 'amqp/failover/disconnected'
|
||||
require 'amqp/failover/logger'
|
||||
require 'amqp/failover/logic'
|
||||
require 'amqp/failover/logic/failed_config'
|
||||
require 'amqp/failover/server_discovery'
|
||||
require 'amqp/failover/version'
|
||||
|
||||
module AMQP
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
# encoding: utf-8
|
||||
|
||||
module AMQP
|
||||
module Failover
|
||||
module BasicClient
|
||||
class OnDisconnect
|
||||
|
||||
attr_accessor :base
|
||||
attr_accessor :failover
|
||||
attr_accessor :configs
|
||||
attr_accessor :logger
|
||||
|
||||
def initialize(base)
|
||||
@base = base
|
||||
@configs = @base.configs
|
||||
@failover_conf = @base.failover_conf
|
||||
@logger = @base.logger
|
||||
end
|
||||
|
||||
def call
|
||||
@logic ||= Logic.new(@configs.configs, @failover_conf.get_primary, @failover_conf.failover_config)
|
||||
if (new_settings = @logic.failover_from(@base.settings))
|
||||
log_message = "Could not connect to or lost connection to server #{@base.settings[:host]}:#{@base.settings[:port]}. " +
|
||||
"Attempting connection to: #{new_settings[:host]}:#{new_settings[:port]}"
|
||||
@logger.error(log_message)
|
||||
@logger.info(log_message)
|
||||
|
||||
if @failover_conf.get_primary == @base.settings
|
||||
FallbackMonitor.monitor(@failover_conf.get_primary) do
|
||||
@base.clean_exit("Primary server (#{@failover_conf.get_primary[:host]}:#{@failover_conf.get_primary[:port]}) is back. " +
|
||||
"Performing clean exit to be relaunched with primary config.")
|
||||
end
|
||||
end
|
||||
|
||||
@base.settings = new_settings
|
||||
@base.reconnect
|
||||
else
|
||||
raise Error, "Could not connect to server #{@base.settings[:host]}:#{@base.settings[:port]}"
|
||||
end
|
||||
end
|
||||
|
||||
end # OnDisconnect
|
||||
end # BasicClient
|
||||
end # Failover
|
||||
end # AMQP
|
||||
43
lib/amqp/failover/disconnected.rb
Normal file
43
lib/amqp/failover/disconnected.rb
Normal file
@@ -0,0 +1,43 @@
|
||||
# encoding: utf-8
|
||||
|
||||
module AMQP
|
||||
module Failover
|
||||
class Disconnected
|
||||
|
||||
attr_accessor :base
|
||||
attr_accessor :failover
|
||||
attr_accessor :configs
|
||||
attr_accessor :logger
|
||||
|
||||
def initialize(base)
|
||||
@base = base
|
||||
@configs = @base.configs
|
||||
@failover_conf = @base.failover_conf
|
||||
@logger = @base.logger
|
||||
end
|
||||
|
||||
def call
|
||||
@logic ||= Logic.new(@configs.configs, @failover_conf.get_primary, @failover_conf.failover_config)
|
||||
if (new_settings = @logic.failover_from(@base.settings))
|
||||
log_message = "Could not connect to or lost connection to server #{@base.settings[:host]}:#{@base.settings[:port]}. " +
|
||||
"Attempting connection to: #{new_settings[:host]}:#{new_settings[:port]}"
|
||||
@logger.error(log_message)
|
||||
@logger.info(log_message)
|
||||
|
||||
if @failover_conf.get_primary == @base.settings
|
||||
ServerDiscovery.monitor(@failover_conf.get_primary) do
|
||||
@base.clean_exit("Primary server (#{@failover_conf.get_primary[:host]}:#{@failover_conf.get_primary[:port]}) is back. " +
|
||||
"Performing clean exit to be relaunched with primary config.")
|
||||
end
|
||||
end
|
||||
|
||||
@base.settings = new_settings
|
||||
@base.reconnect
|
||||
else
|
||||
raise Error, "Could not connect to server #{@base.settings[:host]}:#{@base.settings[:port]}"
|
||||
end
|
||||
end
|
||||
|
||||
end # Disconnected
|
||||
end # Failover
|
||||
end # AMQP
|
||||
@@ -2,12 +2,20 @@
|
||||
|
||||
module AMQP
|
||||
module Failover
|
||||
class Fallback < EM::Connection
|
||||
class ServerDiscovery < EM::Connection
|
||||
|
||||
class << self
|
||||
attr_accessor :connection
|
||||
end
|
||||
|
||||
def self.monitor(conf = {}, &block)
|
||||
if EM.reactor_running?
|
||||
start_monitoring(conf, &block)
|
||||
else
|
||||
EM.run { start_monitoring(conf, &block) }
|
||||
end
|
||||
end
|
||||
|
||||
def initialize(args)
|
||||
@done = args[:done]
|
||||
@timer = args[:timer]
|
||||
@@ -18,15 +26,7 @@ module AMQP
|
||||
@timer.cancel
|
||||
close_connection
|
||||
end
|
||||
|
||||
def self.monitor(conf = {}, &block)
|
||||
if EM.reactor_running?
|
||||
start_monitoring(conf, &block)
|
||||
else
|
||||
EM.run { start_monitoring(conf, &block) }
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def self.start_monitoring(conf = {}, &block)
|
||||
conf = conf.clone
|
||||
conf[:done] = block
|
||||
@@ -39,6 +39,6 @@ module AMQP
|
||||
EM.connect(conf[:host], conf[:port], self, conf)
|
||||
end
|
||||
|
||||
end # Fallback
|
||||
end # ServerDiscovery
|
||||
end # Failover
|
||||
end # AMQP
|
||||
@@ -1,4 +1,4 @@
|
||||
class FallbackHelper < AMQP::Failover::Fallback
|
||||
class ServerDiscoveryHelper < AMQP::Failover::ServerDiscovery
|
||||
|
||||
class << self
|
||||
alias :real_start_monitoring :start_monitoring
|
||||
@@ -2,9 +2,9 @@
|
||||
$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__))
|
||||
|
||||
require 'spec_helper'
|
||||
require 'fallback_helper'
|
||||
require 'server_discovery_helper'
|
||||
|
||||
describe AMQP::Failover::Fallback do
|
||||
describe AMQP::Failover::ServerDiscovery do
|
||||
|
||||
before(:each) do
|
||||
$called = []
|
||||
@@ -15,7 +15,7 @@ describe AMQP::Failover::Fallback do
|
||||
it "should initialize" do
|
||||
EM.run {
|
||||
EM.start_server('127.0.0.1', 9999)
|
||||
@mon = FallbackHelper.monitor(@args) do
|
||||
@mon = ServerDiscoveryHelper.monitor(@args) do
|
||||
$called << :done_block
|
||||
EM.stop_event_loop
|
||||
end
|
||||
@@ -32,7 +32,7 @@ describe AMQP::Failover::Fallback do
|
||||
|
||||
it "should retry on error" do
|
||||
EM.run {
|
||||
@mon = FallbackHelper.monitor(@args) do
|
||||
@mon = ServerDiscoveryHelper.monitor(@args) do
|
||||
$called << :done_block
|
||||
EM.stop_event_loop
|
||||
end
|
||||
@@ -48,4 +48,3 @@ describe AMQP::Failover::Fallback do
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user