<commit_before>module Appsignal
  class Agent
    ACTION = 'log_entries'.freeze

    attr_accessor :aggregator, :thread, :master_pid, :pid, :active, :sleep_time,
                  :transmitter, :subscriber, :paused

    def initialize
      return unless Appsignal.active?
      if Appsignal.config.env == 'development'
        @sleep_time = 10.0
      else
        @sleep_time = 60.0
      end
      @master_pid = Process.pid
      @pid = @master_pid
      @aggregator = Aggregator.new
      @transmitter = Transmitter.new(ACTION)
      subscribe
      start_thread
      Appsignal.logger.info('Started Appsignal agent')
    end

    def start_thread
      Appsignal.logger.debug('Starting agent thread')
      @thread = Thread.new do
        loop do
          send_queue if aggregator.has_transactions?
          Appsignal.logger.debug("Sleeping #{sleep_time}")
          sleep(sleep_time)
        end
      end
    end

    def restart_thread
      stop_thread
      start_thread
    end

    def stop_thread
      if @thread && @thread.alive?
        Appsignal.logger.debug 'Killing agent thread'
        Thread.kill(@thread)
      end
    end<commit_msg>Sleep for a random time when starting thread<commit_after>module Appsignal
  class Agent
    ACTION = 'log_entries'.freeze

    attr_accessor :aggregator, :thread, :master_pid, :pid, :active, :sleep_time,
                  :transmitter, :subscriber, :paused

    def initialize
      return unless Appsignal.active?
      if Appsignal.config.env == 'development'
        @sleep_time = 10.0
      else
        @sleep_time = 60.0
      end
      @master_pid = Process.pid
      @pid = @master_pid
      @aggregator = Aggregator.new
      @transmitter = Transmitter.new(ACTION)
      subscribe
      start_thread
      Appsignal.logger.info('Started Appsignal agent')
    end

    def start_thread
      Appsignal.logger.debug('Starting agent thread')
      @thread = Thread.new do
        sleep(rand(sleep_time))
        loop do
          send_queue if aggregator.has_transactions?
          Appsignal.logger.debug("Sleeping #{sleep_time}")
          sleep(sleep_time)
        end
      end
    end

    def restart_thread
      stop_thread
      start_thread
    end

    def stop_thread
      if @thread && @thread.alive?
        Appsignal.logger.debug 'Killing agent thread'
        Thread.kill(@thread)
      end
    end
{'fraction_non_alphanumeric': 0.08063838723225536, 'fraction_numerical': 0.00503989920201596, 'mean_word_length': 2.223274695534506, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3673184', 'n_tokens_mistral': 725, 'n_tokens_neox': 685, 'n_words': 167}