Class: Adhd::ConnectionBank
Instance Method Summary collapse
- #add_connection(conn) ⇒ Object
-
#initialize ⇒ ConnectionBank
constructor
Manage a bunch of connections for us.
- #rerun(conn) ⇒ Object
- #run_all ⇒ Object
Constructor Details
#initialize ⇒ ConnectionBank
Manage a bunch of connections for us
182 183 184 |
# File 'lib/adhd/reactor.rb', line 182 def initialize @our_connections = [] end |
Instance Method Details
#add_connection(conn) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/adhd/reactor.rb', line 186 def add_connection(conn) # Make sure we have no duplicates @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
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/adhd/reactor.rb', line 202 def rerun(conn) # 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. 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 and 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
222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/adhd/reactor.rb', line 222 def run_all # Go through all connections and run them all # Run within EM.run loop # puts "Connection bank runs all... (#{@our_connections.length} connections)" @our_connections.each do |c| if c.is_closed? or !c.keep_alive? puts "Actually rerun #{c.db_name}..." rerun(c) end end end |