<commit_before>
module RPMContrib
  SUPPORTS_FORK =
    begin
      # jruby has a Kernel.fork method, but it raises NotImplementedError by default
      if Kernel.respond_to?(:fork)
        Kernel.fork { exit! }
        true
      else
        false
      end
    rescue NotImplementedError
      false
    end

  module Instrumentation
    # == Resque Instrumentation
    #
    # Installs a hook to ensure the agent starts manually when the worker
    # starts and also adds the tracer to the process method which executes
    # in the forked task.
    module ResqueInstrumentation
      ::Resque::Job.class_eval do
        include NewRelic::Agent::Instrumentation::ControllerInstrumentation
        
        old_perform_method = instance_method(:perform)

        define_method(:perform) do
          class_name = (payload_class ||self.class).name
          NewRelic::Agent.reset_stats if NewRelic::Agent.respond_to? :reset_stats
          perform_action_with_newrelic_trace(:name => 'perform', :class_name => class_name,
                                             :category => 'OtherTransaction/ResqueJob') do
            old_perform_method.bind(self).call
          end

          if ::RPMContrib::SUPPORTS_FORK
            NewRelic::Agent.shutdown unless defined?(::Resque.before_child_exit)
          else
            NewRelic::Agent.agent.instance_variable_get(:@worker_loop).run_task unless defined?(::Resque.before_child_exit)
          end

        end
      end

      if defined?(::Resque.before_child_exit)
        ::Resque.before_child_exit do |worker|
          if ::RPMContrib::SUPPORTS_FORK
            NewRelic::Agent.shutdown
          else
            NewRelic::Agent.agent.instance_variable_get(:@worker_loop).run_task
          end
        end
      end
    end
  end<commit_msg>Fixed issue when NewRelic agent is not configured to start<commit_after>
module RPMContrib
  SUPPORTS_FORK =
    begin
      # jruby has a Kernel.fork method, but it raises NotImplementedError by default
      if Kernel.respond_to?(:fork)
        Kernel.fork { exit! }
        true
      else
        false
      end
    rescue NotImplementedError
      false
    end

  module Instrumentation
    # == Resque Instrumentation
    #
    # Installs a hook to ensure the agent starts manually when the worker
    # starts and also adds the tracer to the process method which executes
    # in the forked task.
    module ResqueInstrumentation
      def self.flush_metric_data
        return if !NewRelic::Agent.agent.started? || NewRelic::Agent.agent.instance_variable_get(:@worker_loop).nil?
        NewRelic::Agent.agent.instance_variable_get(:@worker_loop).run_task
      end

      ::Resque::Job.class_eval do
        include NewRelic::Agent::Instrumentation::ControllerInstrumentation
        
        old_perform_method = instance_method(:perform)

        define_method(:perform) do
          class_name = (payload_class ||self.class).name
          NewRelic::Agent.reset_stats if NewRelic::Agent.respond_to? :reset_stats
          perform_action_with_newrelic_trace(:name => 'perform', :class_name => class_name,
                                             :category => 'OtherTransaction/ResqueJob') do
            old_perform_method.bind(self).call
          end

          if ::RPMContrib::SUPPORTS_FORK
            NewRelic::Agent.shutdown unless defined?(::Resque.before_child_exit)
          else
            ::RPM::Contrib::Instrumentation::ResqueInstrumentation.flush_metric_data unless defined?(::Resque.before_child_exit)
          end

        end
      end

      if defined?(::Resque.before_child_exit)
        ::Resque.before_child_exit do |worker|
          if ::RPMContrib::SUPPORTS_FORK
            NewRelic::Agent.shutdown
          else
            ::RPM::Contrib::Instrumentation::ResqueInstrumentation.flush_metric_data
          end
        end
      end
    end
  end
{'fraction_non_alphanumeric': 0.07593637762955362, 'fraction_numerical': 0.0, 'mean_word_length': 2.4291996481970095, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2481240', 'n_tokens_mistral': 1106, 'n_tokens_neox': 1091, 'n_words': 242}