Class: Officer::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/officer/client.rb

Instance Method Summary collapse

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 options={}
  @socket_type = options[:socket_type] || 'TCP'
  @socket_file = options[:socket_file] || '/tmp/officer.sock'
  @host = options[:host] || 'localhost'
  @port = options[:port] || 11500
  @namespace = options[:namespace]

  connect
end

Instance Method Details

#connectionsObject



76
77
78
# File 'lib/officer/client.rb', line 76

def connections
  execute :command => 'connections'
end

#disconnectObject



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, options={}
  result = execute :command => 'lock', :name => name_with_ns(name),
    :timeout => options[:timeout], :queue_max => options[:queue_max]
  strip_ns_from_hash result, 'name'
end

#locksObject



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

def locks
  execute :command => 'locks'
end

#my_locksObject



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

#reconnectObject



27
28
29
30
# File 'lib/officer/client.rb', line 27

def reconnect
  disconnect
  connect
end

#resetObject



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

Raises:



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, options={}
  response = lock name, options
  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