Class: WhoCan::ConnectionManager
- Inherits:
-
Object
- Object
- WhoCan::ConnectionManager
- Includes:
- Deferred::Accessors, Logging
- Defined in:
- lib/who_can/connection_manager.rb
Overview
this class will automatically fail-back when the primary is re-established.
This class handles setting up the AMQP connection. You must start the EventMachine reactor before running anything in this class
Keeps two connections open, a primary and secondary, and reacts to changes in their state. In the non-failure case, when a client calls #connection on us, they will receive the primary connection (once it’s been established). If the primary is down, or the connection can’t be established for some reason, then the secondary is returned.
When a failover situation happens (i.e. the primary goes down) the on_failure callback will be fired. When that callback is fired, the users of this class are expected to clean up and wait until the on_recovery deferred is fired. The on_recovery deferred is fired when there is at least one open connection to a broker available.
TODO: determine how long to wait for connection to primary before failing
over to secondary.
Constant Summary collapse
- DEFAULT_OPTIONS =
{ :primary_grace_time => 5.0 }
Instance Attribute Summary collapse
-
#primary ⇒ Object
readonly
Returns the value of attribute primary.
-
#primary_grace_time ⇒ Object
Returns the value of attribute primary_grace_time.
-
#primary_opts ⇒ Object
readonly
Returns the value of attribute primary_opts.
-
#secondary ⇒ Object
readonly
Returns the value of attribute secondary.
-
#secondary_opts ⇒ Object
readonly
Returns the value of attribute secondary_opts.
Instance Method Summary collapse
- #close(*a, &b) ⇒ Object
- #close!(*a, &b) ⇒ Object
- #connection ⇒ Object
-
#initialize(primary_opts, secondary_opts, opts = {}) ⇒ ConnectionManager
constructor
A new instance of ConnectionManager.
- #primary_connected? ⇒ Boolean
- #secondary_connected? ⇒ Boolean
- #start(*a, &b) ⇒ Object
-
#start!(&blk) ⇒ Object
connect to the broker, handle reconnections.
- #usable? ⇒ Boolean (also: #useable?)
Methods included from Logging
Constructor Details
#initialize(primary_opts, secondary_opts, opts = {}) ⇒ ConnectionManager
Returns a new instance of ConnectionManager.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/who_can/connection_manager.rb', line 144 def initialize(primary_opts, secondary_opts, opts={}) opts = opts.with_indifferent_access.reverse_merge(DEFAULT_OPTIONS) @primary_opts = primary_opts @secondary_opts = secondary_opts @primary_grace_time = opts[:primary_grace_time] if @primary_opts.kind_of?(ConnectionWrapper) @primary = @primary_opts @primary.name ||= 'primary' else @primary = ConnectionWrapper.new(primary_opts, :name => 'primary') end if @secondary_opts.kind_of?(ConnectionWrapper) @secondary = @secondary_opts @secondary.name ||= 'secondary' else @secondary = ConnectionWrapper.new(secondary_opts, :name => 'secondary') end @active_connection = nil super() end |
Instance Attribute Details
#primary ⇒ Object (readonly)
Returns the value of attribute primary.
33 34 35 |
# File 'lib/who_can/connection_manager.rb', line 33 def primary @primary end |
#primary_grace_time ⇒ Object
Returns the value of attribute primary_grace_time.
35 36 37 |
# File 'lib/who_can/connection_manager.rb', line 35 def primary_grace_time @primary_grace_time end |
#primary_opts ⇒ Object (readonly)
Returns the value of attribute primary_opts.
33 34 35 |
# File 'lib/who_can/connection_manager.rb', line 33 def primary_opts @primary_opts end |
#secondary ⇒ Object (readonly)
Returns the value of attribute secondary.
33 34 35 |
# File 'lib/who_can/connection_manager.rb', line 33 def secondary @secondary end |
#secondary_opts ⇒ Object (readonly)
Returns the value of attribute secondary_opts.
33 34 35 |
# File 'lib/who_can/connection_manager.rb', line 33 def secondary_opts @secondary_opts end |
Instance Method Details
#close(*a, &b) ⇒ Object
183 184 185 186 |
# File 'lib/who_can/connection_manager.rb', line 183 def close(*a, &b) on_close(&b) super(*a) end |
#close!(*a, &b) ⇒ Object
188 189 190 191 |
# File 'lib/who_can/connection_manager.rb', line 188 def close!(*a, &b) on_close(&b) super(*a) end |
#connection ⇒ Object
193 194 195 |
# File 'lib/who_can/connection_manager.rb', line 193 def connection @active_connection end |
#primary_connected? ⇒ Boolean
197 198 199 |
# File 'lib/who_can/connection_manager.rb', line 197 def primary_connected? primary and primary.connected? end |
#secondary_connected? ⇒ Boolean
201 202 203 |
# File 'lib/who_can/connection_manager.rb', line 201 def secondary_connected? secondary and secondary.connected? end |
#start(*a, &b) ⇒ Object
171 172 173 174 |
# File 'lib/who_can/connection_manager.rb', line 171 def start(*a, &b) on_open(&b) super(*a) end |
#start!(&blk) ⇒ Object
connect to the broker, handle reconnections. barf if transition to :start_up state is not possible
178 179 180 181 |
# File 'lib/who_can/connection_manager.rb', line 178 def start!(&blk) on_open(&b) super(*a) end |
#usable? ⇒ Boolean Also known as: useable?
205 206 207 |
# File 'lib/who_can/connection_manager.rb', line 205 def usable? failed_over? or degraded? or connected? end |