<commit_before>module Karma::Thread

  class ManagedThread
    
    attr_reader :thread

    def self.internal_key_prefix
      'Karma::Thread::ManagedThread_'
    end

    def initialize(blocks = {}, options = {})
      @thread = nil
      @running_sleep_time = options[:running_sleep_time]||1
      blocks[:starting] ||= Proc.new { Karma.logger.debug { "#{$$}::#{Thread.current.to_s} starting" } }
      blocks[:finishing] ||= Proc.new { Karma.logger.debug { "#{$$}::#{Thread.current.to_s} finishing" } }
      blocks[:running] ||= Proc.new { Karma.logger.debug { "#{$$}::#{Thread.current.to_s} running" } }
      blocks[:performance] ||= Proc.new { Karma.logger.debug { "#{$$}::#{Thread.current.to_s} performance" } }

      Thread.abort_on_exception = true #only for debug
      @thread = ::Thread.new do
        begin
          Thread.current[:status] = :initing
          Thread.current[:initing_at] = Time.now
          Thread.current[:last_running_at] = Thread.current[:initing_at]
          Thread.current[:parent_class] = self
          Thread.current[:internal_key] = "#{Karma::Thread::ManagedThread.internal_key_prefix}#{$$}"
          Thread.current[:is_managed_thread] = true
          Thread.current[:status] = :inited
          Thread.current[:thread_index] = options[:thread_index]
          Karma.logger.debug{ "#{$$}::#{Thread.current.to_s} initialized #{Thread.current[:status]}" }
          sleep 0.1 until Thread.current[:status] == :running
          Karma.logger.debug{ "#{$$}::#{Thread.current.to_s} started #{Thread.current[:status]}" }
          outer_block(blocks)
        rescue Exception => e
          Karma.logger.error{ "#{$$}::#{Thread.current.to_s} #{e.message}" }
          raise e
        end
      end
      # @thread[:custom_inspect_block] ||= Proc.new { "#{$$}::#{@thread[:thread_index]} #{Time.now} custom_inspect" }
    end

    def to_s
      "#{@thread.inspect} index:#{@thread[:thread_index]} status:#{@thread[:status]}, initing_at:#{@thread[:initing_at]}, last_running_at:#{@thread[:last_running_at]} (#{Time.now - @thread[:last_running_at]} secs ago)"
    end

    def set_log_level(level)
      @thread[:logger].level = level if @thread[:logger].present?
    end

    def thread_index
      @thread[:thread_index]<commit_msg>log_level<commit_after>module Karma::Thread

  class ManagedThread
    
    attr_reader :thread

    def self.internal_key_prefix
      'Karma::Thread::ManagedThread_'
    end

    def initialize(blocks = {}, options = {})
      @thread = nil
      @running_sleep_time = options[:running_sleep_time]||1
      blocks[:starting] ||= Proc.new { Karma.logger.debug { "#{$$}::#{Thread.current.to_s} starting" } }
      blocks[:finishing] ||= Proc.new { Karma.logger.debug { "#{$$}::#{Thread.current.to_s} finishing" } }
      blocks[:running] ||= Proc.new { Karma.logger.debug { "#{$$}::#{Thread.current.to_s} running" } }
      blocks[:performance] ||= Proc.new { Karma.logger.debug { "#{$$}::#{Thread.current.to_s} performance" } }

      Thread.abort_on_exception = true #only for debug
      @thread = ::Thread.new do
        begin
          Thread.current[:status] = :initing
          Thread.current[:initing_at] = Time.now
          Thread.current[:last_running_at] = Thread.current[:initing_at]
          Thread.current[:parent_class] = self
          Thread.current[:internal_key] = "#{Karma::Thread::ManagedThread.internal_key_prefix}#{$$}"
          Thread.current[:is_managed_thread] = true
          Thread.current[:status] = :inited
          Thread.current[:thread_index] = options[:thread_index]
          Karma.logger.debug{ "#{$$}::#{Thread.current.to_s} initialized #{Thread.current[:status]}" }
          sleep 0.1 until Thread.current[:status] == :running
          Karma.logger.debug{ "#{$$}::#{Thread.current.to_s} started #{Thread.current[:status]}" }
          outer_block(blocks)
        rescue Exception => e
          Karma.logger.error{ "#{$$}::#{Thread.current.to_s} #{e.message}" }
          raise e
        end
      end
      # @thread[:custom_inspect_block] ||= Proc.new { "#{$$}::#{@thread[:thread_index]} #{Time.now} custom_inspect" }
    end

    def to_s
      "#{@thread.inspect} index:#{@thread[:thread_index]} status:#{@thread[:status]}, initing_at:#{@thread[:initing_at]}, last_running_at:#{@thread[:last_running_at]} (#{Time.now - @thread[:last_running_at]} secs ago)"
    end

    def set_log_level(level)
      Karma.logger.level = level
      @thread[:logger].level = level if @thread[:logger].present?
    end

    def thread_index
      @thread[:thread_index]
{'fraction_non_alphanumeric': 0.17420765027322405, 'fraction_numerical': 0.0013114754098360656, 'mean_word_length': 3.349809885931559, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1347752', 'n_tokens_mistral': 1478, 'n_tokens_neox': 1469, 'n_words': 229}