Module: NewRelic::Agent::AgentHelpers::Shutdown

Included in:
NewRelic::Agent::Agent
Defined in:
lib/new_relic/agent/agent_helpers/shutdown.rb

Instance Method Summary collapse

Instance Method Details

#graceful_disconnectObject

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


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/new_relic/agent/agent_helpers/shutdown.rb', line 45

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

#shutdownObject

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


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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

  NewRelic::Agent.agent.health_check.update_status(NewRelic::Agent::HealthCheck::SHUTDOWN) if NewRelic::Agent.agent.health_check.healthy?

  Control.reset
end

#shutdown_serviceObject


63
64
65
66
67
68
69
70
71
# File 'lib/new_relic/agent/agent_helpers/shutdown.rb', line 63

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_disconnectObject


28
29
30
31
32
33
34
35
36
# File 'lib/new_relic/agent/agent_helpers/shutdown.rb', line 28

def untraced_graceful_disconnect
  begin
    NewRelic::Agent.disable_all_tracing do
      graceful_disconnect
    end
  rescue => e
    ::NewRelic::Agent.logger.error(e)
  end
end