Class: ZooKeeper::EventHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/zookeeper/event_handler.rb

Overview

this is the default watcher provided by the zookeeper connection and is used to monitor all watch events on any paths 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 Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (EventHandler) initialize(zookeeper_client)

:nodoc:



16
17
18
19
# File 'lib/zookeeper/event_handler.rb', line 16

def initialize(zookeeper_client)
  @zk = zookeeper_client
  @callbacks = Hash.new { |h,k| h[k] = [] }
end

Instance Attribute Details

- (Object) zk

:nodoc:



12
13
14
# File 'lib/zookeeper/event_handler.rb', line 12

def zk
  @zk
end

Instance Method Details

- (Object) process(event)

:nodoc:



79
80
81
# File 'lib/zookeeper/event_handler.rb', line 79

def process(event)
  handle_process(event)
end

- (ZooKeeper::EventHandlerSubscription) register(path, &block) {|connection, event| ... } Also known as: subscribe

register a path with the handler your block will be called with all events on that path. aliased as #subscribe

Parameters:

  • path (String)

    the path you want to listen to

  • block (Block)

    the block to execute when a watch event happpens

Yields:

  • (connection, event)

    We will call your block with the connection the watch event occured on and the event object

Returns:

See Also:



32
33
34
35
36
# File 'lib/zookeeper/event_handler.rb', line 32

def register(path, &block)
  EventHandlerSubscription.new(self, path, block).tap do |subscription|
    @callbacks[path] << subscription
  end
end

- (Object) register_state_handler(state, &block) {|connection, event| ... }

registers a "state of the connection" handler

Parameters:

  • state (String)

    the state you want to register for

  • block (Block)

    the block to execute on state changes

Yields:

  • (connection, event)

    yields your block with



43
44
45
# File 'lib/zookeeper/event_handler.rb', line 43

def register_state_handler(state, &block)
  register("state_#{state}", &block)
end

- (Object) unregister(*args) Also known as: unsubscribe

Deprecated.

use #unsubscribe on the subscription object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/zookeeper/event_handler.rb', line 59

def unregister(*args)
  if args.first.is_a?(EventHandlerSubscription)
    subscription = args.first
  elsif args.first.is_a?(String) and args[1].is_a?(EventHandlerSubscription)
    subscription = args[1]
  else
    path, index = args[0..1]
    @callbacks[path][index] = nil
    return
  end
  ary = @callbacks[subscription.path]
  if index = ary.index(subscription)
    ary[index] = nil
  end
end

- (Object) unregister_state_handler(*args)

Deprecated.

use #unsubscribe on the subscription object



49
50
51
52
53
54
55
# File 'lib/zookeeper/event_handler.rb', line 49

def unregister_state_handler(*args)
  if args.first.is_a?(EventHandlerSubscription)
    unregister(args.first)
  else
    unregister("state_#{args.first}", args[1])
  end
end