Class: CouchRest::Changes::Observer

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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, options = {})
  @db_name = Config.complete_db_name(db_name)
  info { "Tracking #{db_name}" }
  debug { "Options: #{options.inspect}" } if options.keys.any?
  @options = options
  @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

Parameters:

  • value

    the value to set the attribute logger to.



11
12
13
# File 'lib/couchrest/changes/observer.rb', line 11

def logger=(value)
  @logger = value
end

#sinceObject (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_sequenceObject



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

#listenObject



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(feed_options) 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