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 = {}) { ... } ⇒ 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
104 105 106 107 |
# File 'lib/zk/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 = {}) ⇒ Election::Observer
Constructs an Election::Observer object using self as the connection
114 115 116 |
# File 'lib/zk/client/conveniences.rb', line 114 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
133 134 135 |
# File 'lib/zk/client/conveniences.rb', line 133 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 = {}) { ... } ⇒ 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.
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/zk/client/conveniences.rb', line 86 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 |