Class: NewRelic::Agent::Commands::AgentCommandRouter

Inherits:
Object
  • Object
show all
Defined in:
lib/new_relic/agent/commands/agent_command_router.rb

Defined Under Namespace

Classes: AgentCommandError

Constant Summary collapse

SUCCESS_RESULT =
{}.freeze
ERROR_KEY =
"error"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service, thread_profiler) ⇒ AgentCommandRouter

Returns a new instance of AgentCommandRouter.



19
20
21
22
23
24
25
26
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 19

def initialize(service, thread_profiler)
  @service = service

  @handlers    = Hash.new { |*| Proc.new { |cmd| self.unrecognized_agent_command(cmd) } }

  @handlers['start_profiler'] = Proc.new { |cmd| thread_profiler.handle_start_command(cmd) }
  @handlers['stop_profiler']  = Proc.new { |cmd| thread_profiler.handle_stop_command(cmd) }
end

Instance Attribute Details

#handlersObject (readonly)

Returns the value of attribute handlers.



17
18
19
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 17

def handlers
  @handlers
end

#serviceObject (readonly)

Returns the value of attribute service.



17
18
19
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 17

def service
  @service
end

Instance Method Details

#call_handler_for(agent_command) ⇒ Object



73
74
75
76
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 73

def call_handler_for(agent_command)
  handler = select_handler(agent_command)
  handler.call(agent_command)
end

#error(err) ⇒ Object



69
70
71
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 69

def error(err)
  { ERROR_KEY => err.message}
end

#get_agent_commandsObject



33
34
35
36
37
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 33

def get_agent_commands
  commands = service.get_agent_commands
  NewRelic::Agent.logger.debug "Received get_agent_commands = #{commands.inspect}"
  commands
end

#handle_agent_commandsObject



28
29
30
31
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 28

def handle_agent_commands
  results = invoke_commands(get_agent_commands)
  service.agent_command_results(results) unless results.empty?
end

#invoke_command(agent_command) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 53

def invoke_command(agent_command)
  begin
    call_handler_for(agent_command)
    return success
  rescue AgentCommandError => e
    error(e)
  end
end

#invoke_commands(collector_commands) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 39

def invoke_commands(collector_commands)
  results = {}

  collector_commands.each do |collector_command|
    agent_command = NewRelic::Agent::Commands::AgentCommand.new(collector_command)
    results[agent_command.id.to_s] = invoke_command(agent_command)
  end

  results
end

#select_handler(agent_command) ⇒ Object



78
79
80
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 78

def select_handler(agent_command)
  @handlers[agent_command.name]
end

#successObject



65
66
67
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 65

def success
  SUCCESS_RESULT
end

#unrecognized_agent_command(agent_command) ⇒ Object



82
83
84
# File 'lib/new_relic/agent/commands/agent_command_router.rb', line 82

def unrecognized_agent_command(agent_command)
  NewRelic::Agent.logger.debug("Unrecognized agent command #{agent_command.inspect}")
end