Skip to content

Instantly share code, notes, and snippets.

@bnagy
Created July 2, 2012 03:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bnagy/3030761 to your computer and use it in GitHub Desktop.
Save bnagy/3030761 to your computer and use it in GitHub Desktop.
require 'zk'
require 'msgpack'
class Events
def initialize
@zk = ZK.new
@root_node = '/fuzz'
@node_metadata=MessagePack.pack 'required_workers'=>3
if @zk.stat( @root_node ).exists?
@zk.set @root_node, @node_metadata
else
@zk.create @root_node, @node_metadata
end
end
def run
begin
@zk.register(@root_node) do |event|
case event
when node_changed?
# fetch the latest data and re-set watch
data = @zk.get(@root_node, watch: true).first
puts "Metadata was changed: #{MessagePack.unpack(data)}"
when node_children?
# Get data, reset watch on the root node (NOT the child)
puts "Children changed: #{@zk.children(@root_node, watch: true)}"
end
end
ensure
@zk.close!
end
end
end
Events.new.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment