<commit_before>require 'singleton'
require 'timeout'
require 'json'
require 'restclient'

module Papermill

  class Agent
    include Singleton
    
    # send new request data every 10 seconds
    UPDATE_INTERVAL = 10

    attr_reader :last_sent, :mutex, :config, :logger

    # request timeout threshold for sending data to papermill
    @@request_timeout = 5
    def self.request_timeout
      @@request_timeout
    end

    def start
      @last_sent = Time.now
      @mutex     = Mutex.new
      @config    = Configurator.new
      @logger    = Logger.new
      Thread.abort_on_exception = true

      @worker_thread = Thread.new do
        loop do
          if time_since_last_sent > UPDATE_INTERVAL
            p "sending #{Storage.store.count} requests to papermill..."
            send_data_to_papermill
            @last_sent = Time.now
          end
          sleep_time = seconds_until_next_run
          sleep sleep_time if sleep_time > 0
        end
      end
    end

    def send_data_to_papermill
      begin
        Timeout.timeout(self.class.request_timeout) do
          do_request if !Storage.empty? && config.live_mode
        end
      rescue Timeout::Error => e
        logger.log_exception(e)
      end
    end

    def do_request
      begin
        logger.info("#{Time.now}: Sending #{Storage.size} requests to papermill")
        RestClient.post config.endpoint, { :token => config.token, :payload => jsonify_payload }
        Storage.clear
      rescue RestClient::Exception, Errno::ECONNREFUSED, SocketError => e
        logger.log_exception e
      end
    end


    def time_since_last_sent
      Time.now - last_sent
    end

    def seconds_until_next_run
      UPDATE_INTERVAL - time_since_last_sent
    end

    # Return a json version of the storage array.
    # Also, we'll clear out the storage here
    #
    # TODO: move this to the Storage class.
    def jsonify_payload
      mutex.synchronize do
        begin
          json_data = ::JSON.generate(Storage.store.flatten)
        rescue Exception => e
          logger.log_exception(e)
        end
        return json_data
      end
    end
  end

end
<commit_msg>allow user to set the request timeout setting<commit_after>require 'singleton'
require 'timeout'
require 'json'
require 'restclient'

module Papermill

  class Agent
    include Singleton
    
    # send new request data every 10 seconds
    UPDATE_INTERVAL = 10

    attr_reader :last_sent, :mutex, :config, :logger

    # request timeout threshold for sending data to papermill
    @@request_timeout = 5
    def self.request_timeout
      @@request_timeout
    end

    def start
      @last_sent = Time.now
      @mutex     = Mutex.new
      @config    = Configurator.new
      @logger    = Logger.new
      Thread.abort_on_exception = true

      @worker_thread = Thread.new do
        loop do
          if time_since_last_sent > UPDATE_INTERVAL
            p "sending #{Storage.store.count} requests to papermill..."
            send_data_to_papermill
            @last_sent = Time.now
          end
          sleep_time = seconds_until_next_run
          sleep sleep_time if sleep_time > 0
        end
      end
    end

    def send_data_to_papermill
      begin
        Timeout.timeout(config.setting('request_timout') || self.class.request_timeout) do
          do_request if !Storage.empty? && config.live_mode
        end
      rescue Timeout::Error => e
        logger.log_exception(e)
      end
    end

    def do_request
      begin
        logger.info("#{Time.now}: Sending #{Storage.size} requests to papermill")
        RestClient.post config.endpoint, { :token => config.token, :payload => jsonify_payload }
        Storage.clear
      rescue RestClient::Exception, Errno::ECONNREFUSED, SocketError => e
        logger.log_exception e
      end
    end


    def time_since_last_sent
      Time.now - last_sent
    end

    def seconds_until_next_run
      UPDATE_INTERVAL - time_since_last_sent
    end

    # Return a json version of the storage array.
    # Also, we'll clear out the storage here
    #
    # TODO: move this to the Storage class.
    def jsonify_payload
      mutex.synchronize do
        begin
          json_data = ::JSON.generate(Storage.store.flatten)
        rescue Exception => e
          logger.log_exception(e)
        end
        return json_data
      end
    end
  end

end

{'fraction_non_alphanumeric': 0.0659516202647193, 'fraction_numerical': 0.0027384755819260614, 'mean_word_length': 2.173787110789283, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '942036', 'n_tokens_mistral': 1316, 'n_tokens_neox': 1251, 'n_words': 354}