Class: Adhd::ReplicationConnection

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

Overview

Manages a connection to our CouchDB that maintains continuous replications to remote CouchDB instances. We make sure that the connection does not go down while it is needed.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(our_node, our_db, remote_node, remote_db, event_block) ⇒ ReplicationConnection

Initiate a replication connection, by passing the local and remote nodes and databases.



72
73
74
75
76
77
78
79
80
81
# File 'lib/adhd/models/replication_connection.rb', line 72

def initialize our_node, our_db, remote_node, remote_db, event_block
  @our_node = our_node
  @our_db = our_db
  @remote_node = remote_node
  @remote_db = remote_db
  @name = "#{our_db}->#{remote_db}"
  @keep_alive = true
  @status = "NOTRUNNING"
  @event_block = event_block
end

Instance Attribute Details

#connection_insideObject

Returns the value of attribute connection_inside.



67
68
69
# File 'lib/adhd/models/replication_connection.rb', line 67

def connection_inside
  @connection_inside
end

#nameObject

Returns the value of attribute name.



67
68
69
# File 'lib/adhd/models/replication_connection.rb', line 67

def name
  @name
end

#our_dbObject

Returns the value of attribute our_db.



67
68
69
# File 'lib/adhd/models/replication_connection.rb', line 67

def our_db
  @our_db
end

#our_nodeObject

Returns the value of attribute our_node.



67
68
69
# File 'lib/adhd/models/replication_connection.rb', line 67

def our_node
  @our_node
end

#remote_dbObject

Returns the value of attribute remote_db.



67
68
69
# File 'lib/adhd/models/replication_connection.rb', line 67

def remote_db
  @remote_db
end

#remote_nodeObject

Returns the value of attribute remote_node.



67
68
69
# File 'lib/adhd/models/replication_connection.rb', line 67

def remote_node
  @remote_node
end

Instance Method Details

#close_handlerObject



109
110
111
112
# File 'lib/adhd/models/replication_connection.rb', line 109

def close_handler
  puts "Closed abnormally: #{reason}"
  @status = "NOTRUNNING"
end

#down_for_good(reason) ⇒ Object



114
115
116
117
118
# File 'lib/adhd/models/replication_connection.rb', line 114

def down_for_good(reason)
  if reason
    puts "Closed for good: #{reason}"
  end
end

#event_handler(data) ⇒ Object



103
104
105
106
107
# File 'lib/adhd/models/replication_connection.rb', line 103

def event_handler data
  #puts "Run a crazy sync on db: #{@db_name}"
  #@sync_block.call(data)
  @event_block.call(:rec, data) 
end

#is_closed?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/adhd/models/replication_connection.rb', line 139

def is_closed?
  (@status == "NOTRUNNING")
end

#keep_alive?Boolean

Returns the truth value of the predicate

Returns:

  • (Boolean)


123
124
125
# File 'lib/adhd/models/replication_connection.rb', line 123

def keep_alive?
  @keep_alive
end

#keep_alive_or_kill!Object



127
128
129
130
131
132
133
# File 'lib/adhd/models/replication_connection.rb', line 127

def keep_alive_or_kill!
  if ! keep_alive?
    # Schedule this connection for close
    connection_inside.close_connection_after_writing
    @status = "NOTRUNNING"
  end
end

#killObject



83
84
85
# File 'lib/adhd/models/replication_connection.rb', line 83

def kill
  @keep_alive = false
end

#should_start?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/adhd/models/replication_connection.rb', line 135

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

#startObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/adhd/models/replication_connection.rb', line 87

def start

  puts "Registering the replication connection for: #{@db_name}"
  node_uri = URI.parse(our_node.url)
  
  begin
    puts "Connecting to #{node_uri.host}:#{node_uri.port}"
    EM.connect node_uri.host, node_uri.port, Adhd::ReplicationNotifier, self
    @status = "RUNNING"
  rescue Exception => e
    # TODO: tag nodes as unavailable if we blow up here
    puts "PROBLEM: #{e.message}"
    throw e
  end
end