Class: ZK::EventHandler

Inherits:
Object
  • Object
show all
Includes:
Java::OrgApacheZookeeper::Watcher, Logger
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

Methods included from Logger

#logger, wrapped_logger, wrapped_logger=

Instance Method Details

#clear_outstanding_watch_restrictions!Object

used when establishing a new session



221
222
223
224
225
# File 'lib/zk/event_handler.rb', line 221

def clear_outstanding_watch_restrictions!
  synchronize do
    @outstanding_watches.values.each { |set| set.clear }
  end
end

#closeObject

shut down the EventHandlerSubscriptions



228
229
230
231
232
233
234
# File 'lib/zk/event_handler.rb', line 228

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
90
91
# 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 do
      (@callbacks[path] ||= []) << subscription
    end
  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

Parameters:

  • state (String, :all)

    The state you want to register for or :all to be called back with every state change

  • block (Block)

    the block to execute on state changes

Yields:

  • (event)

    yields your block with



113
114
115
# File 'lib/zk/event_handler.rb', line 113

def register_state_handler(state, &block)
  register(state_key(state), &block)
end

#unregister(*args) ⇒ Object Also known as: unsubscribe

Deprecated.

use #unsubscribe on the subscription object

See Also:

  • ZK::EventHandlerSubscription#unsubscribe


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/zk/event_handler.rb', line 129

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 do
      if @callbacks[path] && @callbacks[path][index]
        @callbacks[path][index] = nil
      end
    end
    return
  end

  synchronize do
    if ary = @callbacks[subscription.path]
      idx = ary.index(subscription) and ary.delete_at(idx)
      if ary.empty?
        @callbacks.delete(subscription.path)
      end
    end
  end

  nil
end

#unregister_state_handler(*args) ⇒ Object

Deprecated.

use #unsubscribe on the subscription object

See Also:

  • ZK::EventHandlerSubscription#unsubscribe


119
120
121
122
123
124
125
# File 'lib/zk/event_handler.rb', line 119

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