Class: Adhd::UpdateNotifierConnection
Overview
Note: Some of manos’s thoughts on how to manage our connections and events.
We should build a class called connection_manager that we ask to build
and listen to connections, as well as route events. Its job is to
re-open them if they are closed or times out, mark nodes as UNAVAILABLE
and notify us back when data (i.e. an update) arrives. It would also be
nifty if each connection was associated with a predicate: once this
predicate is false we can simply close the connection. For example upon
being given control of a different content shard, or a different master
for the shard.
In practice we will have two types of connections: Replicate and Notify.
Instance Attribute Summary collapse
-
#base_url ⇒ Object
Returns the value of attribute base_url.
-
#connection_inside ⇒ Object
Returns the value of attribute connection_inside.
-
#db_name ⇒ Object
Returns the value of attribute db_name.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
- #close_handler ⇒ Object
- #down_for_good(reason) ⇒ Object
- #event_handler(data) ⇒ Object
-
#initialize(node_url, couchdb_server_port, db_name, sync_block) ⇒ UpdateNotifierConnection
constructor
A new instance of UpdateNotifierConnection.
- #is_closed? ⇒ Boolean
-
#keep_alive? ⇒ Boolean
Returns the truth value of the predicate.
- #keep_alive_or_kill! ⇒ Object
- #kill ⇒ Object
- #should_start? ⇒ Boolean
- #start ⇒ Object
Constructor Details
#initialize(node_url, couchdb_server_port, db_name, sync_block) ⇒ UpdateNotifierConnection
Returns a new instance of UpdateNotifierConnection.
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/adhd/reactor.rb', line 92 def initialize(node_url, couchdb_server_port, db_name, sync_block) @node_url = node_url @couchdb_server_port = couchdb_server_port @db_name = db_name @sync_block = sync_block @status = "NOTRUNNING" @base_url = "http://#{@node_url}:#{@couchdb_server_port}" @name = @base_url +"/"+ @db_name @keep_alive = true end |
Instance Attribute Details
#base_url ⇒ Object
Returns the value of attribute base_url.
90 91 92 |
# File 'lib/adhd/reactor.rb', line 90 def base_url @base_url end |
#connection_inside ⇒ Object
Returns the value of attribute connection_inside.
90 91 92 |
# File 'lib/adhd/reactor.rb', line 90 def connection_inside @connection_inside end |
#db_name ⇒ Object
Returns the value of attribute db_name.
90 91 92 |
# File 'lib/adhd/reactor.rb', line 90 def db_name @db_name end |
#name ⇒ Object
Returns the value of attribute name.
90 91 92 |
# File 'lib/adhd/reactor.rb', line 90 def name @name end |
Instance Method Details
#close_handler ⇒ Object
118 119 120 121 |
# File 'lib/adhd/reactor.rb', line 118 def close_handler puts "Closed abnormally: #{reason}" @status = "NOTRUNNING" end |
#down_for_good(reason) ⇒ Object
123 124 125 126 127 |
# File 'lib/adhd/reactor.rb', line 123 def down_for_good(reason) if reason puts "Closed for good: #{reason}" end end |
#event_handler(data) ⇒ Object
113 114 115 116 |
# File 'lib/adhd/reactor.rb', line 113 def event_handler data puts "Run a crazy sync on db: #{@db_name}" @sync_block.call(data) end |
#is_closed? ⇒ Boolean
147 148 149 |
# File 'lib/adhd/reactor.rb', line 147 def is_closed? (@status == "NOTRUNNING") end |
#keep_alive? ⇒ Boolean
Returns the truth value of the predicate
131 132 133 |
# File 'lib/adhd/reactor.rb', line 131 def keep_alive? @keep_alive end |
#keep_alive_or_kill! ⇒ Object
135 136 137 138 139 140 141 |
# File 'lib/adhd/reactor.rb', line 135 def keep_alive_or_kill! if ! keep_alive? # Schedule this connection for close connection_inside.close_connection_after_writing @status = "NOTRUNNING" end end |
#kill ⇒ Object
103 104 105 |
# File 'lib/adhd/reactor.rb', line 103 def kill @keep_alive = false end |
#should_start? ⇒ Boolean
143 144 145 |
# File 'lib/adhd/reactor.rb', line 143 def should_start? !(@status == "RUNNING") end |
#start ⇒ Object
107 108 109 110 111 |
# File 'lib/adhd/reactor.rb', line 107 def start puts "Registering the connection for: #{@db_name}" EM.connect @node_url, @couchdb_server_port, Adhd::DbUpdateNotifier, @db_name, self @status = "RUNNING" end |