Sha256: 2e7310a3af22290dddae4b858837497cdd2121ebf71787d7500caa4edf4f9ffc

Contents?: true

Size: 797 Bytes

Versions: 30

Compression:

Stored size: 797 Bytes

Contents

# docs/examples/events_02.rb

require 'thread'
require 'zk'

class Events
  def initialize
    @zk = ZK.new
    @queue = Queue.new
    @path = '/zk-example-events01'
  end

  def do_something_with(data)
    puts "I was told to say #{data.inspect}"
    @queue.push(:got_event)
  end

  def run
    @zk.register(@path) do |event|
      if event.node_changed? or event.node_created?
        data = @zk.get(@path, watch: true).first    # fetch the latest data and re-set watch
        do_something_with(data)
      end
    end

    begin
      @zk.delete(@path) 
    rescue ZK::Exceptions::NoNode
    end

    @zk.stat(@path, watch: true)
    @zk.create(@path, 'Hello, events!')

    @queue.pop

    @zk.set(@path, "ooh, an update!")

    @queue.pop
  ensure
    @zk.close!
  end
end

Events.new.run

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
zk-1.10.0 docs/examples/events_02.rb
zk-1.9.6 docs/examples/events_02.rb
zk-1.9.5 docs/examples/events_02.rb
zk-1.9.4 docs/examples/events_02.rb
zk-1.9.3 docs/examples/events_02.rb
zk-1.9.2 docs/examples/events_02.rb
zk-1.9.1 docs/examples/events_02.rb
zk-1.9.0 docs/examples/events_02.rb
zk-1.8.0 docs/examples/events_02.rb
zk-1.7.5 docs/examples/events_02.rb
zk-1.7.4 docs/examples/events_02.rb
zk-1.7.3 docs/examples/events_02.rb
zk-1.7.2 docs/examples/events_02.rb
zk-1.7.1 docs/examples/events_02.rb
zk-1.7.0 docs/examples/events_02.rb
zk-1.6.5 docs/examples/events_02.rb
zk-1.6.4 docs/examples/events_02.rb
zk-1.6.3 docs/examples/events_02.rb
zk-1.6.2 docs/examples/events_02.rb
zk-1.6.1 docs/examples/events_02.rb