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.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/officer/client.rb', line 12

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]
  @keep_alive_freq = options[:keep_alive_freq] || 6 # Hz.
  @keep_alive_enabled = options.include?(:keep_alive_enabled) ? options[:keep_alive_enabled] : true
  @thread = nil
  @lock = Mutex.new

  connect
end

Instance Method Details

#connectionsObject



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

#disconnectObject



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

#locksObject



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_locksObject



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

#reconnectObject



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

def reconnect
  disconnect
  connect

  self
end

#resetObject



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

Raises:



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, 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