Created
July 2, 2012 03:07
-
-
Save bnagy/3030761 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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