<commit_before># Wrapper class providing convenient access and application specific handling
# @author <NAME>

require 'hailstorm/support'

class Hailstorm::Support::Thread
  
  # Start a new Ruby thread. The thread is push to Hailstorm.application.threads
  # so they can be joined on later. *args are passed as thread local variables
  # to the newly created thread.
  # @param [Object] *args
  # @return [Thread] the thread object  
  def self.start(*args, &block)
    
    Hailstorm.logger.debug { "#{self}.#{__method__}" }
    if Hailstorm.application.multi_threaded?
      thread = Thread.start(args) do |args|
        Thread.current[:connection] = ActiveRecord::Base.connection
        yield(*args)
      end
      Thread.current[:spawned] ||= []
      Thread.current[:spawned].push(thread)
      
      return thread
    else
      yield(*args)
    end 
  end
  
  # Joins all threads spawned by current thread and clears active connections.
  # @return [Boolean] true if all threads exited gracefully, false otherwise
  def self.join()

    graceful = true
    Hailstorm.logger.debug { "#{self}.#{__method__}" }
    spawned = Thread.current[:spawned]
    until spawned.blank?
      t = spawned.shift()
      begin
        t.join()
      rescue StandardError => e
        graceful = false
        logger.warn(e.message())
        logger.debug { "\n".concat(e.backtrace().join("\n")) }
      ensure
        t[:connection].close() unless t[:connection].nil? 
      end
    end

    return graceful
  end
  
  
end<commit_msg>Updated connection closing and release management - now once a thread finishes execution it closes the associated connection and when waiting for child threads to finish, releases the connection back to the pool.<commit_after># Wrapper class providing convenient access and application specific handling
# @author <NAME>

require 'hailstorm/support'

class Hailstorm::Support::Thread
  
  # Start a new Ruby thread. The thread is push to Hailstorm.application.threads
  # so they can be joined on later. *args are passed as thread local variables
  # to the newly created thread.
  # @param [Object] *args
  # @return [Thread] the thread object  
  def self.start(*args, &block)
    
    Hailstorm.logger.debug { "#{self}.#{__method__}" }
    if Hailstorm.application.multi_threaded?
      thread = Thread.start(args) do |args|
        begin
          yield(*args)
        rescue Object => e
          logger.error(e.message())
          logger.debug { "\n".concat(e.backtrace().join("\n")) }
          raise()
        ensure
          ActiveRecord::Base.connection.close()
        end
      end
      Thread.current[:spawned] ||= []
      Thread.current[:spawned].push(thread)
      
      return thread
    else
      yield(*args)
    end 
  end
  
  # Joins all threads spawned by current thread and clears active connections.
  # @return [Boolean] true if all threads exited gracefully, false otherwise
  def self.join()

    graceful = true
    Hailstorm.logger.debug { "#{self}.#{__method__}" }
    # Give up the current threads connection, so that a child thread waiting
    # on it will get the connection
    ActiveRecord::Base.connection_pool.release_connection()

    spawned = Thread.current[:spawned]
    until spawned.blank?
      t = spawned.shift()
      begin
        t.join()
      rescue StandardError => e
        graceful = false
      end
    end

    return graceful
  end
  
  
end
{'fraction_non_alphanumeric': 0.09807916181606519, 'fraction_numerical': 0.0, 'mean_word_length': 2.752183406113537, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, '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': '4915348', 'n_tokens_mistral': 973, 'n_tokens_neox': 971, 'n_words': 311}