Class: CouchRest::Changes::Observer
- Inherits:
-
Object
- Object
- CouchRest::Changes::Observer
- Defined in:
- lib/couchrest/changes/observer.rb
Overview
NOTE: the sequence stored in the variable @since is a different format depending on which flavor of couchdb is being used. For normal couchdb prior to version 2.0, the sequence is just a number. For bigcouch and new couchdb instances, the sequence is an array.
Instance Attribute Summary collapse
-
#logger ⇒ Object
writeonly
Sets the attribute logger.
-
#since ⇒ Object
readonly
Returns the value of attribute since.
Instance Method Summary collapse
-
#changed(hash = {}, &block) ⇒ Object
triggered whenever a document was changed.
-
#created(hash = {}, &block) ⇒ Object
triggered when a document was newly created.
-
#deleted(hash = {}, &block) ⇒ Object
triggered when a document was deleted.
-
#initialize(db_name, options = {}) ⇒ Observer
constructor
A new instance of Observer.
- #last_sequence ⇒ Object
- #listen ⇒ Object
-
#updated(hash = {}, &block) ⇒ Object
triggered when an existing document was updated.
Constructor Details
#initialize(db_name, options = {}) ⇒ Observer
Returns a new instance of Observer.
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/couchrest/changes/observer.rb', line 14 def initialize(db_name, = {}) @db_name = Config.complete_db_name(db_name) info { "Tracking #{db_name}" } debug { "Options: #{options.inspect}" } if .keys.any? = @db = DatabaseProxy.new(@db_name) setup_sequence_file(@db_name) unless rerun? @since = read_or_reset_sequence(@db_name) else @since = 0 end end |
Instance Attribute Details
#logger=(value) ⇒ Object
Sets the attribute logger
11 12 13 |
# File 'lib/couchrest/changes/observer.rb', line 11 def logger=(value) @logger = value end |
#since ⇒ Object (readonly)
Returns the value of attribute since.
12 13 14 |
# File 'lib/couchrest/changes/observer.rb', line 12 def since @since end |
Instance Method Details
#changed(hash = {}, &block) ⇒ Object
triggered whenever a document was changed
44 45 46 |
# File 'lib/couchrest/changes/observer.rb', line 44 def changed(hash = {}, &block) run_or_define_hook :changed, hash, &block end |
#created(hash = {}, &block) ⇒ Object
triggered when a document was newly created
29 30 31 |
# File 'lib/couchrest/changes/observer.rb', line 29 def created(hash = {}, &block) run_or_define_hook :created, hash, &block end |
#deleted(hash = {}, &block) ⇒ Object
triggered when a document was deleted
34 35 36 |
# File 'lib/couchrest/changes/observer.rb', line 34 def deleted(hash = {}, &block) run_or_define_hook :deleted, hash, &block end |
#last_sequence ⇒ Object
67 68 69 70 |
# File 'lib/couchrest/changes/observer.rb', line 67 def last_sequence hash = @db.changes :limit => 1, :descending => true return hash["last_seq"] end |
#listen ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/couchrest/changes/observer.rb', line 48 def listen info { "Listening to #{@db_name}/_changes starting at sequence #{since}" } last = nil result = @db.changes() do |hash| last = hash @retry_count = 0 callbacks(hash) if hash_for_change?(hash) store_seq(@db_name, hash["seq"]) end raise EOFError # appearently MultiJson has issues with the end of the couch stream. # So sometimes we get a MultiJson::LoadError instead... rescue MultiJson::LoadError, EOFError, RestClient::ServerBrokeConnection => exc error { "Couch #{@db_name}/_changes stream ended - #{exc.class}" } debug { result.inspect } if result debug { last.inspect } if last retry if retry_without_sequence?(result, last) || retry_later? end |
#updated(hash = {}, &block) ⇒ Object
triggered when an existing document was updated
39 40 41 |
# File 'lib/couchrest/changes/observer.rb', line 39 def updated(hash = {}, &block) run_or_define_hook :updated, hash, &block end |