Class: Adhd::UpdateNotifierConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/adhd/reactor.rb

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

Instance Method Summary collapse

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_urlObject

Returns the value of attribute base_url.



90
91
92
# File 'lib/adhd/reactor.rb', line 90

def base_url
  @base_url
end

#connection_insideObject

Returns the value of attribute connection_inside.



90
91
92
# File 'lib/adhd/reactor.rb', line 90

def connection_inside
  @connection_inside
end

#db_nameObject

Returns the value of attribute db_name.



90
91
92
# File 'lib/adhd/reactor.rb', line 90

def db_name
  @db_name
end

#nameObject

Returns the value of attribute name.



90
91
92
# File 'lib/adhd/reactor.rb', line 90

def name
  @name
end

Instance Method Details

#close_handlerObject



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

Returns:

  • (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

Returns:

  • (Boolean)


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

#killObject



103
104
105
# File 'lib/adhd/reactor.rb', line 103

def kill
  @keep_alive = false
end

#should_start?Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/adhd/reactor.rb', line 143

def should_start?
  !(@status == "RUNNING")
end

#startObject



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