Class: RightScale::CommandClient

Inherits:
Object
  • Object
show all
Defined in:
lib/right_agent/command/command_client.rb

Defined Under Namespace

Modules: ConnectionHandler

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket_port, cookie) ⇒ CommandClient

Create client

Parameters

socket_port(Integer)

Socket port on which to connect to agent

cookie(String)

Cookie associated with command server



35
36
37
38
39
# File 'lib/right_agent/command/command_client.rb', line 35

def initialize(socket_port, cookie)
  @socket_port = socket_port
  @cookie = cookie
  @pending = 0
end

Instance Attribute Details

#responseObject

Agent response if any



28
29
30
# File 'lib/right_agent/command/command_client.rb', line 28

def response
  @response
end

Instance Method Details

#send_command(options, verbose = false, timeout = 20, &handler) ⇒ Object

Send command to running agent

Parameters

options(Hash)

Hash of options and command name

options

Command name

options

Other command specific options, passed through to agent

verbose(Boolean)

Whether client should display debug info

timeout(Integer)

Number of seconds we should wait for a reply from the agent

Block

handler: Command results handler

Return

true

Always return true

Raise

RuntimeError

Timed out waiting for result, raised in EM thread



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/right_agent/command/command_client.rb', line 71

def send_command(options, verbose=false, timeout=20, &handler)
  return if @closing
  @last_timeout = timeout
  manage_em = !EM.reactor_running?
  response_handler = lambda do
    EM.stop if manage_em
    handler.call(@response) if handler && @response
    @pending -= 1
    @close_handler.call if @close_handler && @pending == 0
  end
  send_handler = lambda do
    @pending += 1
    command = options.dup
    command[:verbose] = verbose
    command[:timeout] = timeout
    command[:cookie] = @cookie
    EM.next_tick { EM.connect('127.0.0.1', @socket_port, ConnectionHandler, command, self, response_handler) }
    EM.add_timer(timeout) { EM.stop; raise 'Timed out waiting for agent reply' } if manage_em
  end
  if manage_em
    EM.run { send_handler.call }
  else
    send_handler.call
  end
  true
end

#stop(&close_handler) ⇒ Object

Stop command client

Block

Given block gets called back once last response has been received or timeout



45
46
47
48
49
50
51
52
# File 'lib/right_agent/command/command_client.rb', line 45

def stop(&close_handler)
  if @pending > 0
    @close_timeout = EM::Timer.new(@last_timeout) { close_handler.call(timeout=true) }
    @close_handler = lambda { @close_timeout.cancel; close_handler.call(timeout=false) }
  else
    close_handler.call(timeout=false)
  end
end