Module: RightScale::AgentConnection

Defined in:
lib/instance/cook/agent_connection.rb

Overview

Class managing connection to agent

Constant Summary collapse

STOP_TIMEOUT =

Wait up to 20 seconds before forcing disconnection to agent

20

Instance Method Summary collapse

Instance Method Details

#initialize(cookie, thread_name, callback = nil) ⇒ Object

Set command client cookie and initialize responses parser



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/instance/cook/agent_connection.rb', line 34

def initialize(cookie, thread_name, callback=nil)
  @cookie = cookie
  @thread_name = thread_name
  @pending = 0
  @parser = CommandParser.new do |data|
    if callback
      callback.call(data)
    else
      Log.warning("[cook] Unexpected command protocol response '#{data}'") unless data == 'OK'
    end
    @pending -= 1
    on_stopped if @stopped_callback && @pending == 0
  end
end

#on_stoppedObject

Called after all pending responses have been received

Return

true

Always return true



101
102
103
104
105
# File 'lib/instance/cook/agent_connection.rb', line 101

def on_stopped
  close_connection
  @stop_timeout.cancel if @stop_timeout
  @stopped_callback.call
end

#receive_data(data) ⇒ Object

Handle agent response

Return

true

Always return true



72
73
74
75
# File 'lib/instance/cook/agent_connection.rb', line 72

def receive_data(data)
  @parser.parse_chunk(data)
  true
end

#send_command(options) ⇒ 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

Return

true

Always return true



58
59
60
61
62
63
64
65
66
# File 'lib/instance/cook/agent_connection.rb', line 58

def send_command(options)
  return if @stopped_callback
  @pending += 1
  command = options.dup
  command[:cookie] = @cookie
  command[:thread_name] = @thread_name
  send_data(CommandSerializer.dump(command))
  true
end

#stop(&callback) ⇒ Object

Stop command client, wait for all pending commands to finish prior to calling given callback

Return

true

Always return true

Block

called once all pending commands have completed



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/instance/cook/agent_connection.rb', line 85

def stop(&callback)
  send_command(:name => :close_connection)
  @stopped_callback = callback
  Log.info("[cook] Disconnecting from agent (#{@pending} response#{@pending > 1 ? 's' : ''} pending)")
  @stop_timeout = EM::Timer.new(STOP_TIMEOUT) do
    Log.warning("[cook] Time out waiting for responses from agent, forcing disconnection")
    @stop_timeout = nil
    on_stopped
  end
  true
end