Class: CouchWatcher::DatabaseListener
- Inherits:
-
Object
- Object
- CouchWatcher::DatabaseListener
- Defined in:
- lib/couchwatcher/database_listener.rb
Instance Method Summary collapse
- #add_database_callback(callback_proc, include_document) ⇒ Object
- #add_document_callback(document_id, callback_proc, include_document) ⇒ Object
-
#initialize(database_url) ⇒ DatabaseListener
constructor
A new instance of DatabaseListener.
- #remove_database_callback(database_callback) ⇒ Object
- #remove_document_callback(document_callback) ⇒ Object
-
#say(message, color = nil) ⇒ Object
shortcut to say.
- #start_watching(verbose, polling_interval) ⇒ Object
- #stop_watching(verbose) ⇒ Object
Constructor Details
#initialize(database_url) ⇒ DatabaseListener
Returns a new instance of DatabaseListener.
14 15 16 17 18 19 20 21 22 |
# File 'lib/couchwatcher/database_listener.rb', line 14 def initialize(database_url) @database_url = database_url @last_sequence = nil @thread = nil @DatabaseCallback = Struct.new(:callback_proc, :include_document) @database_callbacks = [] @DocumentCallback = Struct.new(:document_id, :callback_proc, :include_document) @document_callbacks = [] end |
Instance Method Details
#add_database_callback(callback_proc, include_document) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/couchwatcher/database_listener.rb', line 24 def add_database_callback(callback_proc, include_document) if !@thread.nil? say "Error: cannot add callbacks to a running listener", :red return nil end database_callback = @DatabaseCallback.new(callback_proc, include_document) @database_callbacks << database_callback return database_callback end |
#add_document_callback(document_id, callback_proc, include_document) ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/couchwatcher/database_listener.rb', line 44 def add_document_callback(document_id, callback_proc, include_document) if !@thread.nil? say "Error: cannot add callbacks to a running listener", :red return nil end document_callback = @DocumentCallback.new(document_id, callback_proc, include_document) @document_callbacks << document_callback return document_callback end |
#remove_database_callback(database_callback) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/couchwatcher/database_listener.rb', line 35 def remove_database_callback(database_callback) if !@thread.nil? say "Error: cannot remove callbacks from a running listener", :red return end @database_callbacks.delete(database_callback) end |
#remove_document_callback(document_callback) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/couchwatcher/database_listener.rb', line 55 def remove_document_callback(document_callback) if !@thread.nil? say "Error: cannot remove callbacks from a running listener", :red return end @document_callbacks.delete(document_callback) end |
#say(message, color = nil) ⇒ Object
shortcut to say
9 10 11 12 |
# File 'lib/couchwatcher/database_listener.rb', line 9 def say(, color=nil) @shell ||= Thor::Shell::Basic.new @shell.say , color end |
#start_watching(verbose, polling_interval) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/couchwatcher/database_listener.rb', line 64 def start_watching(verbose, polling_interval) if !@thread.nil? say "Error: listener is already running listener", :red return end say "Database watching started for: #{@database_url}", :green if verbose @thread = Thread.new do # get the sequence if @last_sequence.nil? data = get_info() if data.nil? || data["update_seq"].nil? say "Error: the database cannnot be watched: #{@database_url}. Check that the couchdb is running, the database exists, and you have adequate permissions.", :red return end @last_sequence = data["update_seq"] end @connection_stopped = false while true #puts "Checking heartbeat: #{@database_url} sequence: #{@last_sequence} connecte" # stopped the connection if @connection_stopped return # the sequence number has been returned so start polling elsif @last_sequence results = get_changes() #puts "Checking heartbeat: #{@database_url} sequence: #{@last_sequence}" results.each do |result| document_id = result["id"] document_rev = result["changes"][0]["rev"] document = result["doc"] # tell a change say "Change detected in database: #{@database_url} document: #{document_id}", :green if verbose # call the database watchers @database_callbacks.each do |callback| if callback.include_document callback.callback_proc.call(@database_url, document_id, document_rev, document) else callback.callback_proc.call(@database_url, document_id, document_rev) end end # call the document watchers @document_callbacks.each do |callback| if (callback.document_id==document_id) if callback.include_document callback.callback_proc.call(@database_url, document_id, document_rev, document) else callback.callback_proc.call(@database_url, document_id, document_rev) end end end end end sleep polling_interval end end end |
#stop_watching(verbose) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/couchwatcher/database_listener.rb', line 135 def stop_watching(verbose) if @thread.nil? say "Error: cannot stop because listener is is not running", :red return end # give the thread time to finish @connection_stopped = true @thread.kill @thread = nil say "Database watching stopped for: #{@database_url}", :green if verbose end |