<commit_before>module Stormtroopers
  class Army
    attr_reader :factory, :threads, :max_threads

    def initialize(config)
      @factory = factory_class(config).new(config[:factory])
      @max_threads = config[:max_threads] || 1
      @threads = []
    end

    def factory_class(config)
      raise ArgumentError, "Factory class or type must be defined" if config[:factory][:class].blank? && config[:factory][:type].blank?
      class_name ||= config[:factory].delete(:class)
      class_name ||= "stormtroopers/#{config[:factory].delete(:type)}_factory".camelize
      class_name.constantize
    end

    def manage
      cleanup
      if threads.count < max_threads
        if trooper = factory.produce
          threads << Thread.new do
            begin
              trooper.run
            ensure
              if defined?(::Mongoid)
                ::Mongoid::IdentityMap.clear
                ::Mongoid.disconnect_sessions
              end
            end
          end<commit_msg>Disconnect the default session<commit_after>module Stormtroopers
  class Army
    attr_reader :factory, :threads, :max_threads

    def initialize(config)
      @factory = factory_class(config).new(config[:factory])
      @max_threads = config[:max_threads] || 1
      @threads = []
    end

    def factory_class(config)
      raise ArgumentError, "Factory class or type must be defined" if config[:factory][:class].blank? && config[:factory][:type].blank?
      class_name ||= config[:factory].delete(:class)
      class_name ||= "stormtroopers/#{config[:factory].delete(:type)}_factory".camelize
      class_name.constantize
    end

    def manage
      cleanup
      if threads.count < max_threads
        if trooper = factory.produce
          threads << Thread.new do
            begin
              trooper.run
            ensure
              if defined?(::Mongoid)
                ::Mongoid::IdentityMap.clear
                ::Mongoid.session(:default).disconnect
              end
            end
          end
{'fraction_non_alphanumeric': 0.10406342913776016, 'fraction_numerical': 0.0009910802775024777, 'mean_word_length': 2.293637846655791, 'pattern_counts': {'":': 0, '<': 9, '<?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': '1971937', 'n_tokens_mistral': 556, 'n_tokens_neox': 539, 'n_words': 120}