Class: ZK::EventHandler
- Inherits:
-
Object
- Object
- ZK::EventHandler
- Includes:
- Java::OrgApacheZookeeper::Watcher, Logging
- Defined in:
- lib/zk/event_handler.rb
Overview
This is the default watcher provided by the zookeeper connection watchers are implemented by adding the :watch => true flag to any #children or #get or #exists calls
you never really need to initialize this yourself
Instance Method Summary collapse
-
#clear_outstanding_watch_restrictions! ⇒ Object
used when establishing a new session.
-
#close ⇒ Object
shut down the EventHandlerSubscriptions.
- #register(path, opts = {}, &block) ⇒ Object (also: #subscribe)
-
#register_state_handler(state, &block) {|event| ... } ⇒ Object
Registers a "state of the connection" handler.
-
#unregister(*args) ⇒ Object
(also: #unsubscribe)
deprecated
Deprecated.
use #unsubscribe on the subscription object
-
#unregister_state_handler(*args) ⇒ Object
deprecated
Deprecated.
use #unsubscribe on the subscription object
Methods included from Logging
Instance Method Details
#clear_outstanding_watch_restrictions! ⇒ Object
used when establishing a new session
213 214 215 216 217 |
# File 'lib/zk/event_handler.rb', line 213 def clear_outstanding_watch_restrictions! synchronize do @outstanding_watches.values.each { |set| set.clear } end end |
#close ⇒ Object
shut down the EventHandlerSubscriptions
220 221 222 223 224 225 226 |
# File 'lib/zk/event_handler.rb', line 220 def close synchronize do @callbacks.values.flatten.each(&:close) @state = :closed clear! end end |
#register(path, opts = {}, &block) ⇒ Object Also known as: subscribe
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/zk/event_handler.rb', line 68 def register(path, opts={}, &block) path = ALL_NODE_EVENTS_KEY if path == :all hash = {:thread => @thread_opt} # gah, ok, handle the 1.0 form case opts when Array, Symbol warn "Deprecated! #{self.class}#register use the :only option instead of passing a symbol or array" hash[:only] = opts when Hash hash.merge!(opts) when nil # no-op else raise ArgumentError, "don't know how to handle options: #{opts.inspect}" end EventHandlerSubscription.new(self, path, block, hash).tap do |subscription| synchronize { @callbacks[path] << subscription } end end |
#register_state_handler(state, &block) {|event| ... } ⇒ Object
Registers a "state of the connection" handler
Valid states are: connecting, associating, connected, auth_failed,
expired_session. Of all of these, you are probably most likely
interested in expired_session
and connecting
, which are fired
when you either lose your session (and have to completely reconnect),
or when there's a temporary loss in connection and Zookeeper recommends
you go into 'safe mode'.
Note that these callbacks are not one-shot like the path callbacks, these will be called back with every relative state event, there is no need to re-register
111 112 113 |
# File 'lib/zk/event_handler.rb', line 111 def register_state_handler(state, &block) register(state_key(state), &block) end |
#unregister(*args) ⇒ Object Also known as: unsubscribe
use #unsubscribe on the subscription object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/zk/event_handler.rb', line 127 def unregister(*args) if args.first.is_a?(EventHandlerSubscription::Base) subscription = args.first elsif args.first.is_a?(String) and args[1].is_a?(EventHandlerSubscription::Base) subscription = args[1] else path, index = args[0..1] synchronize { @callbacks[path][index] = nil } return end synchronize do ary = @callbacks[subscription.path] idx = ary.index(subscription) and ary.delete_at(idx) @callbacks.delete(subscription.path) if ary.empty? end nil end |
#unregister_state_handler(*args) ⇒ Object
use #unsubscribe on the subscription object
117 118 119 120 121 122 123 |
# File 'lib/zk/event_handler.rb', line 117 def unregister_state_handler(*args) if args.first.is_a?(EventHandlerSubscription::Base) unregister(args.first) else unregister(state_key(args.first), args[1]) end end |