Module: ZK::Client::Conveniences
- Included in:
- Threaded
- Defined in:
- lib/zk/client/conveniences.rb
Overview
Convenience methods for creating instances of the cluster coordination objects ZK provides, using the current connection.
Mixed into Threaded
Instance Method Summary collapse
-
#election_candidate(name, data, opts = {}) ⇒ Election::Candidate
Constructs an Election::Candidate object using self as the connection.
-
#election_observer(name, opts = {}) ⇒ Election::Observer
Constructs an Election::Observer object using self as the connection.
-
#locker(name) ⇒ Locker::ExclusiveLocker
(also: #exclusive_locker)
Creates a new locker based on the name you provide, using this client as the connection.
-
#queue(name) ⇒ MessageQueue
creates a new message queue of name
name
. -
#shared_locker(name) ⇒ Locker::SharedLocker
create a new shared locking instance based on the name given.
-
#with_lock(name, opts = {}) {|lock| ... } ⇒ Object
Convenience method for acquiring a lock then executing a code block.
Instance Method Details
#election_candidate(name, data, opts = {}) ⇒ Election::Candidate
Constructs an Election::Candidate object using self as the connection
122 123 124 125 |
# File 'lib/zk/client/conveniences.rb', line 122 def election_candidate(name, data, opts={}) opts = opts.merge(:data => data) ZK::Election::Candidate.new(self, name, opts) end |
#election_observer(name, opts = {}) ⇒ Election::Observer
Constructs an Election::Observer object using self as the connection
132 133 134 |
# File 'lib/zk/client/conveniences.rb', line 132 def election_observer(name, opts={}) ZK::Election::Observer.new(self, name, opts) end |
#locker(name) ⇒ Locker::ExclusiveLocker Also known as: exclusive_locker
Creates a new locker based on the name you provide, using this client as the connection.
49 50 51 |
# File 'lib/zk/client/conveniences.rb', line 49 def locker(name) Locker.exclusive_locker(self, name) end |
#queue(name) ⇒ MessageQueue
The message queue has some scalability limitations. For heavy-duty message processing, the author recommends investigating a purpose-built solution.
creates a new message queue of name name
151 152 153 |
# File 'lib/zk/client/conveniences.rb', line 151 def queue(name) MessageQueue.new(self, name) end |
#shared_locker(name) ⇒ Locker::SharedLocker
create a new shared locking instance based on the name given
61 62 63 |
# File 'lib/zk/client/conveniences.rb', line 61 def shared_locker(name) Locker.shared_locker(self, name) end |
#with_lock(name, opts = {}) {|lock| ... } ⇒ Object
Convenience method for acquiring a lock then executing a code block. This will block the caller until the lock is acquired, and release the lock when the block is exited.
Options are the same as for #lock with the addition of
:mode
, documented below.
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/zk/client/conveniences.rb', line 103 def with_lock(name, opts={}, &b) opts = opts.dup mode = opts.delete(: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(opts, &b) else locker(name).with_lock(opts, &b) end end |