Module: ZK::Client::Conveniences

Included in:
Base
Defined in:
lib/z_k/client/conveniences.rb

Instance Method Summary collapse

Instance Method Details

#defer(callable = nil, &block) ⇒ 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)

Arguments

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



16
17
18
# File 'lib/z_k/client/conveniences.rb', line 16

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.



104
105
106
107
# File 'lib/z_k/client/conveniences.rb', line 104

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.



112
113
114
# File 'lib/z_k/client/conveniences.rb', line 112

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

see ZK::Locker::ExclusiveLocker

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")
# => #<ZK::Locker::ExclusiveLocker:0x102034cf8 ...>


55
56
57
# File 'lib/z_k/client/conveniences.rb', line 55

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({:some_data => "that is yaml serializable"})


127
128
129
# File 'lib/z_k/client/conveniences.rb', line 127

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")
# => #<ZK::Locker::SharedLocker:0x102034cf8 ...>


72
73
74
# File 'lib/z_k/client/conveniences.rb', line 72

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)


89
90
91
92
93
94
95
96
97
98
99
# File 'lib/z_k/client/conveniences.rb', line 89

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