lib/new_relic/agent/agent.rb in newrelic_rpm-2.11.1 vs lib/new_relic/agent/agent.rb in newrelic_rpm-2.11.2.beta
- old
+ new
@@ -51,11 +51,19 @@
#
# * It clears any metrics carried over from the parent process
# * Restarts the sampler thread if necessary
# * Initiates a new agent run and worker loop unless that was done
# in the parent process and +:force_reconnect+ is not true
- #
+ #
+ # Options:
+ # * <tt>:force_reconnect => true</tt> to force the spawned process to
+ # establish a new connection, such as when forking a long running process.
+ # The default is false--it will only connect to the server if the parent
+ # had not connected.
+ # * <tt>:keep_retrying => false</tt> if we try to initiate a new
+ # connection, this tells me to only try it once so this method returns
+ # quickly if there is some kind of latency with the server.
def after_fork(options={})
# @connected gets false after we fail to connect or have an error
# connecting. @connected has nil if we haven't finished trying to connect.
# or we didn't attempt a connection because this is the master process
@@ -68,11 +76,14 @@
log.debug "Detected that the worker thread is not running in #$$. Restarting."
# Clear out stats that are left over from parent process
reset_stats
- start_worker_thread(options[:force_reconnect])
+
+ # Don't ever check to see if this is a spawner. If we're in a forked process
+ # I'm pretty sure we're not also forking new instances.
+ start_worker_thread(options.merge(:check_for_spawner => false))
@stats_engine.start_sampler_thread
end
# True if we have initialized and completed 'start'
def started?
@@ -202,11 +213,11 @@
control.log! "No license key found. Please edit your newrelic.yml file and insert your license key.", :error
elsif control.license_key.length != 40
control.log! "Invalid license key: #{control.license_key}", :error
else
# Do the connect in the foreground if we are in sync mode
- NewRelic::Agent.disable_all_tracing { connect(false) } if control.sync_startup
+ NewRelic::Agent.disable_all_tracing { connect(:keep_retrying => false) } if control.sync_startup
# Start the event loop and initiate connection if necessary
start_worker_thread
# Our shutdown handler needs to run after other shutdown handlers
@@ -238,21 +249,23 @@
private
def collector
@collector ||= control.server
end
- # Try to launch the worker thread and connect to the server
- def start_worker_thread(force_reconnect=false)
+ # Try to launch the worker thread and connect to the server.
+ #
+ # See #connect for a description of connection_options.
+ def start_worker_thread(connection_options = {})
log.debug "Creating RPM worker thread."
@worker_thread = Thread.new do
begin
NewRelic::Agent.disable_all_tracing do
# We try to connect. If this returns false that means
# the server rejected us for a licensing reason and we should
# just exit the thread. If it returns nil
# that means it didn't try to connect because we're in the master.
- connect if !@connected or force_reconnect
+ connect(connection_options)
if @connected
# disable transaction sampling if disabled by the server and we're not in dev mode
if !control.developer_mode? && !@should_send_samples
@transaction_sampler.disable
end
@@ -325,20 +338,34 @@
# there's a bad license key.
#
# Set keep_retrying=false to disable retrying and return asap, such as when
# invoked in the foreground. Otherwise this runs until a successful
# connection is made, or the server rejects us.
+ #
+ # * <tt>:keep_retrying => false</tt> to only try to connect once, and
+ # return with the connection set to nil. This ensures we may try again
+ # later (default true).
+ # * <tt>force_reconnect => true</tt> if you want to establish a new connection
+ # to the server before running the worker loop. This means you get a separate
+ # agent run and RPM sees it as a separate instance (default is false).
+ # * <tt>:check_for_spawner => false</tt> to omit the check to see if we are
+ # an application spawner. We detect the spawner and stop the agent so we don't
+ # report stats from a spawner. You don't want to do this check if you _know_
+ # you are not in a spawner (default is true).
- def connect(keep_retrying = true)
+ def connect(options)
+ return if @connected && !options[:force_reconnect]
+ keep_retrying = options[:keep_retrying].nil? || options[:keep_retrying]
+ check_for_spawner = options[:check_for_spawner].nil? || options[:check_for_spawner]
# wait a few seconds for the web server to boot, necessary in development
connect_retry_period = keep_retrying ? 10 : 0
connect_attempts = 0
@agent_id = nil
begin
sleep connect_retry_period.to_i
# Running in the Passenger or Unicorn spawners?
- if $0 =~ /ApplicationSpawner|unicorn.* master/
+ if check_for_spawner && $0 =~ /ApplicationSpawner|^unicorn\S* master/
log.debug "Process is master spawner (#$0) -- don't connect to RPM service"
@connected = nil
return
else
log.debug "Connecting Process to RPM: #$0"