Class: Officer::Client
- Inherits:
-
Object
- Object
- Officer::Client
- Defined in:
- lib/officer/client.rb
Instance Method Summary collapse
- #connections ⇒ Object
- #disconnect ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #lock(name, options = {}) ⇒ Object
- #locks ⇒ Object
- #my_locks ⇒ Object
- #reconnect ⇒ Object
- #reset ⇒ Object
- #unlock(name) ⇒ Object
- #with_lock(name, options = {}) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
17 18 19 20 21 22 23 24 25 |
# File 'lib/officer/client.rb', line 17 def initialize ={} @socket_type = [:socket_type] || 'TCP' @socket_file = [:socket_file] || '/tmp/officer.sock' @host = [:host] || 'localhost' @port = [:port] || 11500 @namespace = [:namespace] connect end |
Instance Method Details
#connections ⇒ Object
76 77 78 |
# File 'lib/officer/client.rb', line 76 def connections execute :command => 'connections' end |
#disconnect ⇒ Object
32 33 34 35 |
# File 'lib/officer/client.rb', line 32 def disconnect @socket.close if @socket @socket = nil end |
#lock(name, options = {}) ⇒ Object
37 38 39 40 41 |
# File 'lib/officer/client.rb', line 37 def lock name, ={} result = execute :command => 'lock', :name => name_with_ns(name), :timeout => [:timeout], :queue_max => [:queue_max] strip_ns_from_hash result, 'name' end |
#locks ⇒ Object
72 73 74 |
# File 'lib/officer/client.rb', line 72 def locks execute :command => 'locks' end |
#my_locks ⇒ Object
80 81 82 83 84 |
# File 'lib/officer/client.rb', line 80 def my_locks result = execute :command => 'my_locks' result['value'] = result['value'].map {|name| strip_ns(name)} result end |
#reconnect ⇒ Object
27 28 29 30 |
# File 'lib/officer/client.rb', line 27 def reconnect disconnect connect end |
#reset ⇒ Object
68 69 70 |
# File 'lib/officer/client.rb', line 68 def reset execute :command => 'reset' end |
#unlock(name) ⇒ Object
43 44 45 46 |
# File 'lib/officer/client.rb', line 43 def unlock name result = execute :command => 'unlock', :name => name_with_ns(name) strip_ns_from_hash result, 'name' end |
#with_lock(name, options = {}) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/officer/client.rb', line 48 def with_lock name, ={} response = lock name, result = response['result'] queue = (response['queue'] || []).join ',' raise LockTimeoutError.new("queue=#{queue}") if result == 'timed_out' raise LockQueuedMaxError.new("queue=#{queue}") if result == 'queue_maxed' raise LockError unless %w(acquired already_acquired).include?(result) begin yield ensure # Deal with nested with_lock calls. Only the outer most call should tell the server to unlock. if result == 'acquired' response = unlock name raise UnlockError unless response['result'] == 'released' end end end |