<commit_before>class DockerRunner
  def self.run(options)
    Docker.options[:read_timeout] = 3600
    Docker.options[:chunk_size] = 1

    # This will fail if there is another container with the same name
    begin
      c = Docker::Container.create("Cmd" => ['/bin/bash', '-l', '-c', options[:command]],
        "User" => "scraper",
        "Image" => options[:image_name],
        "name" => options[:container_name])
    rescue Excon::Errors::SocketError => e
      yield :internal, "Morph internal error: Could not connect to Docker server: #{e}\n"
      yield :internal, "Requeueing...\n"
      raise "Could not connect to Docker server: #{e}"
    end

    # TODO the local path will be different if docker isn't running through Vagrant (i.e. locally)
    # HACK to detect vagrant installation in crude way
    if Rails.root.to_s =~ /\/var\/www/
      local_root_path = Rails.root
    else
      local_root_path = "/vagrant"
    end

    begin
      c.start("Binds" => [
        "#{local_root_path}/#{options[:repo_path]}:/repo:ro",
        "#{local_root_path}/#{options[:data_path]}:/data"
      ])
      puts "Running docker container..."
      c.attach(logs: true) do |s,c|
        yield s,c
      end
    ensure
      # This appears to be giving a broken pipe (Errno::EPIPE) sometimes
      if c.json["State"]["Running"]
        c.kill 
      end
      # Scraper should already have finished now. We're just using this to return the scraper status code
      status_code = c.wait["StatusCode"]

      # Clean up after ourselves
      c.delete
    end

    status_code
  end
end<commit_msg>One less api call required to return exit code<commit_after>class DockerRunner
  def self.run(options)
    Docker.options[:read_timeout] = 3600
    Docker.options[:chunk_size] = 1

    # This will fail if there is another container with the same name
    begin
      c = Docker::Container.create("Cmd" => ['/bin/bash', '-l', '-c', options[:command]],
        "User" => "scraper",
        "Image" => options[:image_name],
        "name" => options[:container_name])
    rescue Excon::Errors::SocketError => e
      yield :internal, "Morph internal error: Could not connect to Docker server: #{e}\n"
      yield :internal, "Requeueing...\n"
      raise "Could not connect to Docker server: #{e}"
    end

    # TODO the local path will be different if docker isn't running through Vagrant (i.e. locally)
    # HACK to detect vagrant installation in crude way
    if Rails.root.to_s =~ /\/var\/www/
      local_root_path = Rails.root
    else
      local_root_path = "/vagrant"
    end

    begin
      c.start("Binds" => [
        "#{local_root_path}/#{options[:repo_path]}:/repo:ro",
        "#{local_root_path}/#{options[:data_path]}:/data"
      ])
      puts "Running docker container..."
      c.attach(logs: true) do |s,c|
        yield s,c
      end
    ensure
      state = c.json["State"]
      c.kill if state["Running"]
      status_code = state["ExitCode"]

      # Clean up after ourselves
      c.delete
    end

    status_code
  end
end
{'fraction_non_alphanumeric': 0.1279527559055118, 'fraction_numerical': 0.0032808398950131233, 'mean_word_length': 2.612559241706161, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1315924', 'n_tokens_mistral': 969, 'n_tokens_neox': 935, 'n_words': 280}