I am using this gem: https://github.com/zk-ruby/zk
for connecting to zookeeper, and get
and set
commands are working correctly.
I have tried adding a watcher client using the sample zookeeper client that comes along with the server build, and all works well there. I can add a watcher and when I update some znode from another client, the client is notified.
But I wish to integrate a watcher in ruby code, I am not getting any events when data on the node is changed.
This is a sample class:
class ZkConnect attr_accessor :z, :sub, :k include Singleton @z = nil @sub = nil @watcher = nil @callback = nil @watcher = nil def initialize initialize_conns end def initialize_conns @z = ZK.new("localhost:2181") Rails.logger.info @z.inspect # Rails.logger.info z.get_children(:path => "/system/production") # Rails.logger.info z.get(:path => "/system/production")[:data] # @sub = @z.register("/mypath") do |event, zookeeper_client| # Rails.logger.info "got an event" # Rails.logger.info "got an event on: #{event.inspect} #{zookeeper_client.inspect}" # end # Rails.logger.info @sub.inspect w = watch Rails.logger.info "dsmcldsm: #{w.inspect}" end # def watcher_callback # @callback ||= Proc.new do |event| # Rails.logger.info "dcbskcn : #{event.inspect}" # # Set new watcher # watch # # Rediscover # # discover # end # Rails.logger.info "nckdj cudsnciu sdcn: #{@callback.inspect}" # return @callback # end def watch # return if @z.nil? Rails.logger.info "synapse: setting watch at " # @watcher = @z.register("/mypath", &watcher_callback) @watcher = @z.register("/mypath") do |event| Rails.logger.info "dcbskcn : #{event.inspect}" puts "dcbskcn : #{event.inspect}" watch end Rails.logger.info "cudsnciu: #{@watcher.inspect}" return @watcher # Verify that we actually set up the watcher. # unless @zk.exists?(@discovery['path'], :watch => true) # log.error "synapse: zookeeper watcher path #{@discovery['path']} does not exist!" # zk_cleanup # end # log.debug "synapse: set watch at #{@discovery['path']}" end end
I took some code from here https://github.com/airbnb/synapse/blob/master/lib/synapse/service_watcher/zookeeper.rb
for this, but didn't help.
I tried it on a rails project, tried running on thin
and passenger
, though I don't see how it would help.
I also don't see eventmachine as a dependency for this gem, so I don't really know how callbacks would be received.
1 Answers
Answers 1
I read changelog and decided to put a @z.stat("/mypath", :watch => true)
after registering the watcher. And it started working. No idea why.
0 comments:
Post a Comment