Class: ZK::EventHandler

Inherits:
Object
  • Object
show all
Includes:
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

Constant Summary

Instance Method Summary collapse

Instance Method Details

#register(path, opts = {}, &block) ⇒ Object Also known as: subscribe



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/zk/event_handler.rb', line 51

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'.

Yields:

  • (event)

    yields your block with



88
89
90
# File 'lib/zk/event_handler.rb', line 88

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


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/zk/event_handler.rb', line 104

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)
  end

  nil
end

#unregister_state_handler(*args) ⇒ Object

Deprecated.

use #unsubscribe on the subscription object

See Also:

  • ZK::EventHandlerSubscription#unsubscribe


94
95
96
97
98
99
100
# File 'lib/zk/event_handler.rb', line 94

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