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
# File 'lib/officer/client.rb', line 17

def initialize options={}
  options.reverse_merge! :host => 'localhost', :port => 11500

  @host = options[:host]
  @port = options[:port]

  connect
end

Instance Method Details

#lock(name, options = {}) ⇒ Object



31
32
33
# File 'lib/officer/client.rb', line 31

def lock name, options={}
  execute({:command => 'lock', :name => name, :timeout => options[:timeout]}.to_json)
end

#reconnectObject



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

def reconnect
  disconnect
  connect
end

#resetObject



57
58
59
# File 'lib/officer/client.rb', line 57

def reset
  execute({:command => 'reset'}.to_json)
end

#unlock(name) ⇒ Object



35
36
37
# File 'lib/officer/client.rb', line 35

def unlock name
  execute({:command => 'unlock', :name => name}.to_json)
end

#with_lock(name, options = {}) ⇒ Object

Raises:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/officer/client.rb', line 39

def with_lock name, options={}
  response = lock name, options
  result = response['result']
  
  raise LockTimeoutError if result == 'timed_out'
  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