mirror of
https://github.com/jimeh/amqp-failover.git
synced 2026-02-19 10:56:44 +00:00
Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5c1f55dd66 | |||
| 18c89b1e0f | |||
| bc69e8970e | |||
| 561a554b3f | |||
| c9f389ce84 | |||
| 1b792bf78e | |||
| bc33a32cf1 | |||
| d464bea237 | |||
| 8fb23e10aa | |||
| 7480532298 | |||
| d03ac850a5 | |||
| 12deba3dca |
2
LICENSE
2
LICENSE
@@ -1,4 +1,4 @@
|
|||||||
Copyright (c) 2011 Jim Myhrberg & Global Personals, Ltd.
|
Copyright (c) 2011 Global Personals, Ltd.
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining
|
Permission is hereby granted, free of charge, to any person obtaining
|
||||||
a copy of this software and associated documentation files (the
|
a copy of this software and associated documentation files (the
|
||||||
|
|||||||
28
README.md
28
README.md
@@ -3,6 +3,11 @@
|
|||||||
Add multi-server support with failover and fallback to the [amqp](https://github.com/ruby-amqp/amqp) gem. Failover is configured by providing multiple servers/configurations to `AMQP.start` or `AMQP.connect`. Both methods will still accept the same options input as they always have, they simply now support additional forms of options which when used, enables the failover features.
|
Add multi-server support with failover and fallback to the [amqp](https://github.com/ruby-amqp/amqp) gem. Failover is configured by providing multiple servers/configurations to `AMQP.start` or `AMQP.connect`. Both methods will still accept the same options input as they always have, they simply now support additional forms of options which when used, enables the failover features.
|
||||||
|
|
||||||
|
|
||||||
|
## Installation ##
|
||||||
|
|
||||||
|
gem install amqp-failover
|
||||||
|
|
||||||
|
|
||||||
## Basic Usage ##
|
## Basic Usage ##
|
||||||
|
|
||||||
require 'mq'
|
require 'mq'
|
||||||
@@ -80,25 +85,8 @@ I would recommend you test the failover functionality in your own infrastructure
|
|||||||
* Send me a pull request. Bonus points for topic branches.
|
* Send me a pull request. Bonus points for topic branches.
|
||||||
|
|
||||||
|
|
||||||
## Liccense and Copyright ##
|
## Licensing and Attribution ##
|
||||||
|
|
||||||
Copyright (c) 2011 Jim Myhrberg & Global Personals, Ltd.
|
Copyright (c) 2011 Global Personals, Ltd.
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining
|
Released under the MIT license as detailed in the [LICENSE](https://github.com/jimeh/amqp-failover/blob/master/LICENSE) file. Developed by [Jim Myhrberg](http://jimeh.me/) during work on [White Label Dating](http://www.whitelabeldating.com/), while employed by [Global Personals Ltd](http://www.globalpersonals.co.uk). Global Personals Ltd have kindly agreed to the extraction and release of this software under the license terms above.
|
||||||
a copy of this software and associated documentation files (the
|
|
||||||
"Software"), to deal in the Software without restriction, including
|
|
||||||
without limitation the rights to use, copy, modify, merge, publish,
|
|
||||||
distribute, sublicense, and/or sell copies of the Software, and to
|
|
||||||
permit persons to whom the Software is furnished to do so, subject to
|
|
||||||
the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be
|
|
||||||
included in all copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
||||||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
||||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
||||||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ Gem::Specification.new do |s|
|
|||||||
s.add_runtime_dependency 'amqp', '>= 0.7.0'
|
s.add_runtime_dependency 'amqp', '>= 0.7.0'
|
||||||
|
|
||||||
s.add_development_dependency 'rake', '>= 0.8.7'
|
s.add_development_dependency 'rake', '>= 0.8.7'
|
||||||
s.add_development_dependency 'rack-test', '>= 0.5.6'
|
|
||||||
s.add_development_dependency 'rspec', '>= 2.1.0'
|
s.add_development_dependency 'rspec', '>= 2.1.0'
|
||||||
s.add_development_dependency 'yard', '>= 0.6.3'
|
s.add_development_dependency 'yard', '>= 0.6.3'
|
||||||
s.add_development_dependency 'json', '>= 1.5.0'
|
s.add_development_dependency 'json', '>= 1.5.0'
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ 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(defaults.merge(hash)))
|
self.replace(defaults.merge(symbolize_keys(hash)))
|
||||||
self.last_fail = last_fail_date if last_fail_date
|
self.last_fail = last_fail_date if last_fail_date
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -2,6 +2,6 @@
|
|||||||
|
|
||||||
module AMQP
|
module AMQP
|
||||||
class Failover
|
class Failover
|
||||||
VERSION = "0.0.1"
|
VERSION = "0.0.3"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -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