class Mmtrix::Agent::Commands::AgentCommandRouter
Constants
- ERROR_KEY
- SUCCESS_RESULT
Attributes
backtrace_service[RW]
handlers[R]
thread_profiler_session[RW]
xray_session_collection[RW]
Public Class Methods
new(event_listener=nil)
click to toggle source
# File lib/mmtrix/agent/commands/agent_command_router.rb, line 24 def initialize(event_listener=nil) @handlers = Hash.new { |*| Proc.new { |cmd| self.unrecognized_agent_command(cmd) } } @backtrace_service = Threading::BacktraceService.new(event_listener) @thread_profiler_session = ThreadProfilerSession.new(@backtrace_service) @xray_session_collection = XraySessionCollection.new(@backtrace_service, event_listener) @handlers['start_profiler'] = Proc.new { |cmd| thread_profiler_session.handle_start_command(cmd) } @handlers['stop_profiler'] = Proc.new { |cmd| thread_profiler_session.handle_stop_command(cmd) } @handlers['active_xray_sessions'] = Proc.new { |cmd| xray_session_collection.handle_active_xray_sessions(cmd) } if event_listener event_listener.subscribe(:before_shutdown, &method(:on_before_shutdown)) end end
Public Instance Methods
active_xray_command?(commands)
click to toggle source
# File lib/mmtrix/agent/commands/agent_command_router.rb, line 58 def active_xray_command?(commands) commands.any? {|command| command.name == 'active_xray_sessions'} end
call_handler_for(agent_command)
click to toggle source
# File lib/mmtrix/agent/commands/agent_command_router.rb, line 145 def call_handler_for(agent_command) handler = select_handler(agent_command) handler.call(agent_command) end
check_for_and_handle_agent_commands()
click to toggle source
# File lib/mmtrix/agent/commands/agent_command_router.rb, line 45 def check_for_and_handle_agent_commands commands = get_agent_commands stop_xray_sessions unless active_xray_command?(commands) results = invoke_commands(commands) mmtrix_service.agent_command_results(results) unless results.empty? end
error(err)
click to toggle source
# File lib/mmtrix/agent/commands/agent_command_router.rb, line 141 def error(err) { ERROR_KEY => err.message } end
get_agent_commands()
click to toggle source
# File lib/mmtrix/agent/commands/agent_command_router.rb, line 105 def get_agent_commands commands = mmtrix_service.get_agent_commands Mmtrix::Agent.logger.debug "Received get_agent_commands = #{commands.inspect}" commands.map {|collector_command| AgentCommand.new(collector_command)} end
harvest!()
click to toggle source
# File lib/mmtrix/agent/commands/agent_command_router.rb, line 68 def harvest! profiles = [] profiles += harvest_from_xray_session_collection profiles += harvest_from_thread_profiler_session log_profiles(profiles) profiles end
harvest_from_thread_profiler_session()
click to toggle source
# File lib/mmtrix/agent/commands/agent_command_router.rb, line 87 def harvest_from_thread_profiler_session if self.thread_profiler_session.ready_to_harvest? self.thread_profiler_session.stop(true) [self.thread_profiler_session.harvest] else [] end end
harvest_from_xray_session_collection()
click to toggle source
# File lib/mmtrix/agent/commands/agent_command_router.rb, line 83 def harvest_from_xray_session_collection self.xray_session_collection.harvest_thread_profiles end
invoke_command(agent_command)
click to toggle source
# File lib/mmtrix/agent/commands/agent_command_router.rb, line 124 def invoke_command(agent_command) begin call_handler_for(agent_command) return success rescue AgentCommandError => e Mmtrix::Agent.logger.debug(e) error(e) end end
invoke_commands(agent_commands)
click to toggle source
# File lib/mmtrix/agent/commands/agent_command_router.rb, line 111 def invoke_commands(agent_commands) results = {} agent_commands.each do |agent_command| results[agent_command.id.to_s] = invoke_command(agent_command) end results end
log_profiles(profiles)
click to toggle source
# File lib/mmtrix/agent/commands/agent_command_router.rb, line 96 def log_profiles(profiles) if profiles.empty? ::Mmtrix::Agent.logger.debug "No thread profiles with data found to send." else profile_descriptions = profiles.map { |p| p.to_log_description } ::Mmtrix::Agent.logger.debug "Sending thread profiles [#{profile_descriptions.join(", ")}]" end end
merge!(*args)
click to toggle source
We don’t currently support merging thread profiles that failed to send back into the AgentCommandRouter
, so we just no-op this method. Same with reset! - we don’t support asynchronous cancellation of a running thread profile or X-Ray session currently.
# File lib/mmtrix/agent/commands/agent_command_router.rb, line 80 def merge!(*args); end
mmtrix_service()
click to toggle source
# File lib/mmtrix/agent/commands/agent_command_router.rb, line 41 def mmtrix_service Mmtrix::Agent.instance.service end
on_before_shutdown(*args)
click to toggle source
# File lib/mmtrix/agent/commands/agent_command_router.rb, line 62 def on_before_shutdown(*args) if self.thread_profiler_session.running? self.thread_profiler_session.stop(true) end end
reset!()
click to toggle source
# File lib/mmtrix/agent/commands/agent_command_router.rb, line 81 def reset!; end
select_handler(agent_command)
click to toggle source
# File lib/mmtrix/agent/commands/agent_command_router.rb, line 150 def select_handler(agent_command) @handlers[agent_command.name] end
stop_xray_sessions()
click to toggle source
# File lib/mmtrix/agent/commands/agent_command_router.rb, line 54 def stop_xray_sessions self.xray_session_collection.stop_all_sessions end
success()
click to toggle source
# File lib/mmtrix/agent/commands/agent_command_router.rb, line 137 def success SUCCESS_RESULT end
unrecognized_agent_command(agent_command)
click to toggle source
# File lib/mmtrix/agent/commands/agent_command_router.rb, line 154 def unrecognized_agent_command(agent_command) Mmtrix::Agent.logger.debug("Unrecognized agent command #{agent_command.inspect}") end