Sha256: 3e259504ad522238829512eea9405b97f3288eb46a89e92504b5e54ada2b277b

Contents?: true

Size: 1.41 KB

Versions: 10

Compression:

Stored size: 1.41 KB

Contents

# This file is distributed under New Relic's license terms.
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
# frozen_string_literal: true

module NewRelic
  module Agent
    class Harvester
      attr_accessor :starting_pid

      # Inject target for after_fork call to avoid spawning thread in tests
      def initialize(events, after_forker = NewRelic::Agent)
        # We intentionally don't set our pid as started at this point.
        # Startup routines must call mark_started when they consider us set!
        @starting_pid = nil
        @after_forker = after_forker

        events&.subscribe(:start_transaction, &method(:on_transaction))
      end

      def on_transaction(*_)
        return unless restart_in_children_enabled? &&
          needs_restart? &&
          harvest_thread_enabled?

        restart_harvest_thread
      end

      def mark_started(pid = Process.pid)
        @starting_pid = pid
      end

      def needs_restart?(pid = Process.pid)
        @starting_pid != pid
      end

      def restart_in_children_enabled?
        NewRelic::Agent.config[:restart_thread_in_children]
      end

      def harvest_thread_enabled?
        !NewRelic::Agent.config[:disable_harvest_thread] && !NewRelic::Agent.config[:'serverless_mode.enabled']
      end

      def restart_harvest_thread
        @after_forker.after_fork(:force_reconnect => true)
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
newrelic_rpm-9.16.0 lib/new_relic/agent/harvester.rb
newrelic_rpm-9.15.0 lib/new_relic/agent/harvester.rb
newrelic_rpm-9.14.0 lib/new_relic/agent/harvester.rb
newrelic_rpm-9.13.0 lib/new_relic/agent/harvester.rb
newrelic_rpm-9.12.0 lib/new_relic/agent/harvester.rb
newrelic_rpm-9.11.0 lib/new_relic/agent/harvester.rb
newrelic_rpm-9.10.2 lib/new_relic/agent/harvester.rb
newrelic_rpm-9.10.1 lib/new_relic/agent/harvester.rb
newrelic_rpm-9.10.0 lib/new_relic/agent/harvester.rb
newrelic_rpm-9.9.0 lib/new_relic/agent/harvester.rb