<commit_before>require_relative "../shared/logging_module"

module FastlaneCI
  # super class for all fastlane.ci workers
  # Subclass this class, and implement `work` and `timeout`
  class WorkerBase
    include FastlaneCI::Logging

    attr_accessor :should_stop

    def initialize
      self.should_stop = false
      Thread.new do
        until self.should_stop
          sleep(self.timeout)
          begin
            self.work unless self.should_stop
          rescue StandardError => ex
            puts("[#{self.class} Exception]: #{ex}: ")
            puts(ex.backtrace.join("\n"))
          end
        end
      end
    end

    def work
      not_implemented(__method__)
    end

    def provider_type
      not_implemented(__method__)
    end

    def die!
      logger.debug("Stopping worker")
      @should_stop = true
    end

    # Timeout in seconds
    def timeout
      not_implemented(__method__)
    end
  end
end
<commit_msg>Catch more exception when threads crash<commit_after>require_relative "../shared/logging_module"

module FastlaneCI
  # super class for all fastlane.ci workers
  # Subclass this class, and implement `work` and `timeout`
  class WorkerBase
    include FastlaneCI::Logging

    attr_accessor :should_stop

    def initialize
      self.should_stop = false
      # TODO: investigate what `abort_on_exception` does
      #   and what to use it for
      # Thread.abort_on_exception = true

      Thread.new do
        until self.should_stop
          Kernel.sleep(self.timeout)

          # We have the `work` inside a `begin rescue`
          # so that if something fails, the thread still is alive
          begin
            self.work unless self.should_stop
          rescue Exception => ex
            puts("[#{self.class} Exception]: #{ex}: ")
            puts(ex.backtrace.join("\n"))
          end
        end
      end
    end

    def work
      not_implemented(__method__)
    end

    def provider_type
      not_implemented(__method__)
    end

    def die!
      logger.debug("Stopping worker")
      @should_stop = true
    end

    # Timeout in seconds
    def timeout
      not_implemented(__method__)
    end
  end
end

{'fraction_non_alphanumeric': 0.07146129180027486, 'fraction_numerical': 0.0, 'mean_word_length': 2.1606367583212736, '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': '884837', 'n_tokens_mistral': 615, 'n_tokens_neox': 611, 'n_words': 180}