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.
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/officer/client.rb', line 12 def initialize ={} @socket_type = [:socket_type] || 'TCP' @socket_file = [:socket_file] || '/tmp/officer.sock' @host = [:host] || 'localhost' @port = [:port] || 11500 @namespace = [:namespace] @keep_alive_freq = [:keep_alive_freq] || 6 # Hz. @keep_alive_enabled = .include?(:keep_alive_enabled) ? [:keep_alive_enabled] : true @thread = nil @lock = Mutex.new connect end |
Instance Method Details
#connections ⇒ Object
108 109 110 111 112 113 114 115 116 |
# File 'lib/officer/client.rb', line 108 def connections result = execute :command => 'connections' if result['result'] != 'connections' force_shutdown end result end |
#disconnect ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/officer/client.rb', line 33 def disconnect @lock.synchronize do @thread.terminate if @thread @thread = nil @socket.close if @socket @socket = nil end self end |
#lock(name, options = {}) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/officer/client.rb', line 45 def lock name, ={} result = execute :command => 'lock', :name => name_with_ns(name), :timeout => [:timeout], :queue_max => [:queue_max] strip_ns_from_hash result, 'name' if result['name'] != name || !%w(acquired already_acquired timed_out queue_maxed).include?(result['result']) force_shutdown end result end |
#locks ⇒ Object
98 99 100 101 102 103 104 105 106 |
# File 'lib/officer/client.rb', line 98 def locks result = execute :command => 'locks' if result['result'] != 'locks' force_shutdown end result end |
#my_locks ⇒ Object
118 119 120 121 122 123 124 125 126 127 |
# File 'lib/officer/client.rb', line 118 def my_locks result = execute :command => 'my_locks' result['value'] = result['value'].map {|name| strip_ns(name)} if result['result'] != 'my_locks' force_shutdown end result end |
#reconnect ⇒ Object
26 27 28 29 30 31 |
# File 'lib/officer/client.rb', line 26 def reconnect disconnect connect self end |
#reset ⇒ Object
88 89 90 91 92 93 94 95 96 |
# File 'lib/officer/client.rb', line 88 def reset result = execute :command => 'reset' if result['result'] != 'reset_succeeded' force_shutdown end result end |
#unlock(name) ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/officer/client.rb', line 57 def unlock name result = execute :command => 'unlock', :name => name_with_ns(name) strip_ns_from_hash result, 'name' if result['name'] != name || !%w(released release_failed).include?(result['result']) force_shutdown end result end |
#with_lock(name, options = {}) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/officer/client.rb', line 68 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 |