<commit_before>at_exit do
  Juici::App.shutdown
end

module Juici
  class App
    @@watchers = []

    def self.shutdown
      ::Juici.dbgp "Shutting down Juici"
      @@watchers.each do |watcher|
        ::Juici.dbgp "Killing #{watcher.inspect}"
        watcher.kill
        watcher.join
        ::Juici.dbgp "Dead:   #{watcher.inspect}"
      end

      shutdown_build_queue
    end

    attr_reader :opts
    def initialize(opts={})
      @opts = opts
      # NOTE: this happening before we start a build queue is important, it
      # means we can't start any more workers and get tied in knots
      # clear_stale_children
      #
      # Urgh
      init_build_queue
      reload_unfinished_work
      start_workers
    end

    def spawn_watcher
      @@watchers << Watcher.start!
    end

  private

    def init_build_queue
      $build_queue ||= BuildQueue.new
    end

    def self.shutdown_build_queue
      $build_queue.shutdown!
      $build_queue = nil

      # Find any remaining children and kill them
      # Ensure that any killed builds will be retried
    end

    def start_workers
      no_workers = opts[:workers] || 1
      warn "More than 1 worker is liable to do strange things" if no_workers > 1
      no_workers.times do
        spawn_watcher
      end
    end

    def reload_unfinished_work
      # At this point no workers have started yet, we can safely assume that
      # :started means aborted
      Build.where(:status.in => [:started, :waiting]).each do |build|
        $build_queue << build
      end
    end

  end
end<commit_msg>Only kill the build queue if we have one<commit_after>at_exit do
  Juici::App.shutdown
end

module Juici
  class App
    @@watchers = []

    def self.shutdown
      ::Juici.dbgp "Shutting down Juici"
      @@watchers.each do |watcher|
        ::Juici.dbgp "Killing #{watcher.inspect}"
        watcher.kill
        watcher.join
        ::Juici.dbgp "Dead:   #{watcher.inspect}"
      end

      shutdown_build_queue
    end

    attr_reader :opts
    def initialize(opts={})
      @opts = opts
      # NOTE: this happening before we start a build queue is important, it
      # means we can't start any more workers and get tied in knots
      # clear_stale_children
      #
      # Urgh
      init_build_queue
      reload_unfinished_work
      start_workers
    end

    def spawn_watcher
      @@watchers << Watcher.start!
    end

  private

    def init_build_queue
      $build_queue ||= BuildQueue.new
    end

    def self.shutdown_build_queue
      if $build_queue
        $build_queue.shutdown!
        $build_queue = nil
      end

      # Find any remaining children and kill them
      # Ensure that any killed builds will be retried
    end

    def start_workers
      no_workers = opts[:workers] || 1
      warn "More than 1 worker is liable to do strange things" if no_workers > 1
      no_workers.times do
        spawn_watcher
      end
    end

    def reload_unfinished_work
      # At this point no workers have started yet, we can safely assume that
      # :started means aborted
      Build.where(:status.in => [:started, :waiting]).each do |build|
        $build_queue << build
      end
    end

  end
end
{'fraction_non_alphanumeric': 0.06904092471102781, 'fraction_numerical': 0.0018744142455482662, 'mean_word_length': 2.221327967806841, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, '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': '3256809', 'n_tokens_mistral': 1003, 'n_tokens_neox': 958, 'n_words': 310}