Class: Adhd::ConnectionBank
Overview
Manage a bunch of connections for us
Instance Method Summary collapse
-
#add_connection(conn) ⇒ Object
Add a connection to the ConnectionBank, making sure we have no duplicates.
-
#initialize ⇒ ConnectionBank
constructor
A new instance of ConnectionBank.
-
#rerun(conn) ⇒ Object
When a connection is down, we check to see if it wants to be kept alive, and restart it; otherwise we remove it from the list.
-
#run_all ⇒ Object
Go through all connections and run them all Run within EM.run loop.
Constructor Details
#initialize ⇒ ConnectionBank
Returns a new instance of ConnectionBank.
158 159 160 |
# File 'lib/adhd/reactor.rb', line 158 def initialize @our_connections = [] end |
Instance Method Details
#add_connection(conn) ⇒ Object
Add a connection to the ConnectionBank, making sure we have no duplicates.
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/adhd/reactor.rb', line 164 def add_connection(conn) @our_connections.each do |c| if conn.name == c.name return end end # If it is happy to run, add it to the list and start it! # if conn.keep_alive? @our_connections << conn # Register the teardown handler for when the end comes... # conn.on_teardown(|c| { rerun(c) }) end end |
#rerun(conn) ⇒ Object
When a connection is down, we check to see if it wants to be kept alive, and restart it; otherwise we remove it from the list.
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/adhd/reactor.rb', line 183 def rerun(conn) if conn.keep_alive? begin conn.start rescue Exception => e conn.down_for_good(e) end else # It seems we have died of natural causes # XXX: is it true that Ruby does not throw an exception for EOF? # Otherwise we will never see this conn.keep_alive_or_kill! @our_connections.delete(conn) conn.down_for_good(nil) end end |
#run_all ⇒ Object
Go through all connections and run them all Run within EM.run loop
204 205 206 207 208 209 210 211 212 |
# File 'lib/adhd/reactor.rb', line 204 def run_all @our_connections.each do |c| if c.is_closed? or !c.keep_alive? puts "Actually rerun #{c.db_name}..." rerun(c) end end end |