Class: CouchRest::Changes::Observer

Inherits:
Object
  • Object
show all
Defined in:
lib/couchrest/changes/observer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db_name, options = {}) ⇒ Observer

Returns a new instance of Observer.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/couchrest/changes/observer.rb', line 6

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
  unless @db = CouchRest.new(Config.couch_host).database(db_name)
    logger.error "Database #{db_name} not found!"
    raise RuntimeError "Database #{db_name} not found!"
  end
  read_seq(Config.seq_file) unless rerun?
  check_seq
end

Instance Attribute Details

#logger=(value) ⇒ Object

Sets the attribute logger

Parameters:

  • value

    the value to set the attribute logger to.



4
5
6
# File 'lib/couchrest/changes/observer.rb', line 4

def logger=(value)
  @logger = value
end

Instance Method Details

#changed(hash = {}, &block) ⇒ Object

triggered whenever a document was changed



35
36
37
# File 'lib/couchrest/changes/observer.rb', line 35

def changed(hash = {}, &block)
  run_or_define_hook :changed, hash, &block
end

#created(hash = {}, &block) ⇒ Object

triggered when a document was newly created



20
21
22
# File 'lib/couchrest/changes/observer.rb', line 20

def created(hash = {}, &block)
  run_or_define_hook :created, hash, &block
end

#deleted(hash = {}, &block) ⇒ Object

triggered when a document was deleted



25
26
27
# File 'lib/couchrest/changes/observer.rb', line 25

def deleted(hash = {}, &block)
  run_or_define_hook :deleted, hash, &block
end

#last_sequenceObject



59
60
61
62
# File 'lib/couchrest/changes/observer.rb', line 59

def last_sequence
  hash = db.changes :limit => 1, :descending => true
  return hash["last_seq"]
end

#listenObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/couchrest/changes/observer.rb', line 39

def listen
  info "listening..."
  debug "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(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
  info "Couch stream ended."
  debug result.inspect
  debug last.inspect
  retry if retry_without_sequence?(result, last) || retry_later?
end

#updated(hash = {}, &block) ⇒ Object

triggered when an existing document was updated



30
31
32
# File 'lib/couchrest/changes/observer.rb', line 30

def updated(hash = {}, &block)
  run_or_define_hook :updated, hash, &block
end