Class: Syncrony::Observer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, path) ⇒ Observer

Returns a new instance of Observer.



9
10
11
12
13
# File 'lib/syncrony/observer.rb', line 9

def initialize(client, path)
  @client = client
  @path = path
  @running = true
end

Instance Attribute Details

#runningObject

Returns the value of attribute running.



7
8
9
# File 'lib/syncrony/observer.rb', line 7

def running
  @running
end

Instance Method Details

#cancelObject



37
38
39
# File 'lib/syncrony/observer.rb', line 37

def cancel
  @running = false
end

#run {|value, @path, info| ... } ⇒ Object

Yields:

  • (value, @path, info)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/syncrony/observer.rb', line 15

def run(&handler)
  begin
    info = @client.get(@path)
    value = info.value
    index = info.etcd_index
  rescue Etcd::KeyNotFound
    info = nil
    value = nil
    index = nil
  end

  yield value, @path, info
  
  while @running
    watch = @client.watch(@path, :index => index ? index + 1 : nil)
    if @running
      index = watch.etcd_index
      yield watch.value, @path, watch
    end
  end
end