<commit_before>require 'set'

module Foodtaster
  class RSpecRun
    def initialize
      @required_vm_names = Set.new
      @client = nil
      @server_process = nil
      @stopped = false
    end

    def require_vm(vm_name)
      @required_vm_names.add(vm_name.to_sym)
    end

    def required_vm_names
      @required_vm_names
    end

    def get_vm(vm_name)
      Foodtaster::Vm.new(vm_name, @client)
    end

    def start
      at_exit { self.stop }

      start_server_and_connect_client
      prepare_required_vms
    end

    def stop
      return if @stopped

      puts "" # newline after rspec output
      shutdown_required_vms if Foodtaster.config.shutdown_vms
      terminate_server
      @stopped = true
    end

    def client
      @client
    end

    class << self
      @instance = nil

      def current
        @instance ||= self.new
      end
    end

    private

    def prepare_required_vms
      self.required_vm_names.each { |vm_name| get_vm(vm_name).prepare }
    end

    def shutdown_required_vms
      self.required_vm_names.each { |vm_name| get_vm(vm_name).shutdown }
    end
<commit_msg>handle SIGINT and SIGTERM during rspec run<commit_after>require 'set'

module Foodtaster
  class RSpecRun
    def initialize
      @required_vm_names = Set.new
      @client = nil
      @server_process = nil
      @stopped = false
    end

    def require_vm(vm_name)
      @required_vm_names.add(vm_name.to_sym)
    end

    def required_vm_names
      @required_vm_names
    end

    def get_vm(vm_name)
      Foodtaster::Vm.new(vm_name, @client)
    end

    def start
      setup_signal_handlers
      start_server_and_connect_client
      prepare_required_vms
    end

    def stop
      return if @stopped

      puts "" # newline after rspec output
      shutdown_required_vms if Foodtaster.config.shutdown_vms
      terminate_server
      @stopped = true
    end

    def client
      @client
    end

    class << self
      @instance = nil

      def current
        @instance ||= self.new
      end
    end

    private

    def setup_signal_handlers
      terminator = proc {
        self.stop
      }

      at_exit(&terminator)
      trap("INT", &terminator)
      trap("TERM", &terminator)
    end

    def prepare_required_vms
      self.required_vm_names.each { |vm_name| get_vm(vm_name).prepare }
    end

    def shutdown_required_vms
      self.required_vm_names.each { |vm_name| get_vm(vm_name).shutdown }
    end

{'fraction_non_alphanumeric': 0.06341463414634146, 'fraction_numerical': 0.0, 'mean_word_length': 2.2596026490066223, 'pattern_counts': {'":': 0, '<': 7, '<?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': '2459478', 'n_tokens_mistral': 829, 'n_tokens_neox': 803, 'n_words': 178}