Module: ZK::Client::Conveniences

Included in:
Threaded
Defined in:
lib/zk/client/conveniences.rb

Overview

EXTENSIONS

convenience methods for dealing with zookeeper (rm -rf, mkdir -p, etc)

Instance Method Summary collapse

Instance Method Details

#defer(callable = nil) { ... } ⇒ Object

Queue an operation to be run on an internal threadpool. You may either provide an object that responds_to?(:call) or pass a block. There is no mechanism for retrieving the result of the operation, it is purely fire-and-forget, so the user is expected to make arrangements for this in their code.

An ArgumentError will be raised if +callable+ does not respond_to?(:call)

Parameters:

  • callable (#call) (defaults to: nil)

    an object that respond_to?(:call), takes precedence over a given block

Yields:

  • [] the block that should be run in the threadpool, if callable isn't given



21
22
23
# File 'lib/zk/client/conveniences.rb', line 21

def defer(callable=nil, &block)
  @threadpool.defer(callable, &block)
end

#election_candidate(name, data, opts = {}) ⇒ Object

Convenience method for constructing a ZK::Election::Candidate object using this Client connection, the given election +name+ and +data+.



100
101
102
103
# File 'lib/zk/client/conveniences.rb', line 100

def election_candidate(name, data, opts={})
  opts = opts.merge(:data => data)
  ZK::Election::Candidate.new(self, name, opts)
end

#election_observer(name, opts = {}) ⇒ Object

Convenience method for constructing a ZK::Election::Observer object using this Client connection, and the given election +name+.



108
109
110
# File 'lib/zk/client/conveniences.rb', line 108

def election_observer(name, opts={})
  ZK::Election::Observer.new(self, name, opts)
end

#locker(name) ⇒ Object

creates a new locker based on the name you send in

returns a ZK::Locker::ExclusiveLocker instance using this Client and provided lock name

==== Arguments

  • name name of the lock you wish to use

==== Examples

zk.locker("blah") # => #



51
52
53
# File 'lib/zk/client/conveniences.rb', line 51

def locker(name)
  Locker.exclusive_locker(self, name)
end

#queue(name) ⇒ Object

creates a new message queue of name +name+

returns a ZK::MessageQueue object

==== Arguments

  • name the name of the queue

==== Examples

zk.queue("blah").publish(=> "that is yaml serializable")



123
124
125
# File 'lib/zk/client/conveniences.rb', line 123

def queue(name)
  MessageQueue.new(self, name)
end

#shared_locker(name) ⇒ Object

create a new shared locking instance based on the name given

returns a ZK::Locker::SharedLocker instance using this Client and provided lock name

==== Arguments

  • name name of the lock you wish to use

==== Examples

zk.shared_locker("blah") # => #



68
69
70
# File 'lib/zk/client/conveniences.rb', line 68

def shared_locker(name)
  Locker.shared_locker(self, name)
end

#with_lock(name, opts = {}, &b) ⇒ Object

Convenience method for acquiring a lock then executing a code block. This will block the caller until the lock is acquired.

==== Arguments

  • name: the name of the lock to use
  • :mode: either :shared or :exclusive, defaults to :exclusive

==== Examples

zk.with_lock('foo') do # this code is executed while holding the lock end

Raises:

  • (ArgumentError)


85
86
87
88
89
90
91
92
93
94
95
# File 'lib/zk/client/conveniences.rb', line 85

def with_lock(name, opts={}, &b)
  mode = opts[:mode] || :exclusive

  raise ArgumentError, ":mode option must be either :shared or :exclusive, not #{mode.inspect}" unless [:shared, :exclusive].include?(mode)

  if mode == :shared
    shared_locker(name).with_lock(&b)
  else
    locker(name).with_lock(&b)
  end
end