module NewRelic::Agent::AgentHelpers::Shutdown

Public Instance Methods

graceful_disconnect() click to toggle source

This method contacts the server to send remaining data and let the server know that the agent is shutting down - this allows us to do things like accurately set the end of the lifetime of the process

If this process comes from a parent process, it will not disconnect, so that the parent process can continue to send data

# File lib/new_relic/agent/agent_helpers/shutdown.rb, line 42
def graceful_disconnect
  if connected?
    begin
      @service.request_timeout = 10

      @events.notify(:before_shutdown)
      transmit_data_types
      shutdown_service

      ::NewRelic::Agent.logger.debug('Graceful disconnect complete')
    rescue Timeout::Error, StandardError => e
      ::NewRelic::Agent.logger.debug("Error when disconnecting #{e.class.name}: #{e.message}")
    end
  else
    ::NewRelic::Agent.logger.debug('Bypassing graceful disconnect - agent not connected')
  end
end
shutdown() click to toggle source

Attempt a graceful shutdown of the agent, flushing any remaining data.

# File lib/new_relic/agent/agent_helpers/shutdown.rb, line 11
def shutdown
  return unless started?

  ::NewRelic::Agent.logger.info('Starting agent shutdown')

  stop_event_loop
  trap_signals_for_litespeed
  untraced_graceful_disconnect
  revert_to_default_configuration

  @started = nil
  Control.reset
end
shutdown_service() click to toggle source
# File lib/new_relic/agent/agent_helpers/shutdown.rb, line 60
def shutdown_service
  if @connected_pid == $$ && !@service.kind_of?(NewRelic::Agent::NewRelicService)
    ::NewRelic::Agent.logger.debug('Sending New Relic service agent run shutdown message')
    @service.shutdown
  else
    ::NewRelic::Agent.logger.debug("This agent connected from parent process #{@connected_pid}--not sending " \
      'shutdown')
  end
end
untraced_graceful_disconnect() click to toggle source
# File lib/new_relic/agent/agent_helpers/shutdown.rb, line 25
def untraced_graceful_disconnect
  begin
    NewRelic::Agent.disable_all_tracing do
      graceful_disconnect
    end
  rescue => e
    ::NewRelic::Agent.logger.error(e)
  end
end