<commit_before>require "moya/version"

module Moya

  # Initialize rails in the development environment.
  def self.initialize_rails!
    require File.dirname(__FILE__) << "/moya/config/environment"
  end

  # Uses Process.spawn to setup and spin up the crichton demo service rails server
  def self.spawn_rails_process!(port = 3000, initializer_directory = nil)
    rails_root = File.dirname(__FILE__) << '/moya'
    env_vars = env_vars_hash(port, initializer_directory)

    Dir.chdir(rails_root) do
      system(env_vars, 'bundle exec rake config')
      system(env_vars, 'bundle exec rake db:setup')
    end

    @pid = Process.spawn(env_vars, "bundle exec rails server --port #{port}", chdir: "#{rails_root}")
    wait_for_response!("http://localhost:#{port}")

  ensure
    return @pid
  end

  private

  # Waits up to 30 seconds for a response to root from the passed in url
  def self.wait_for_response!(url)
    connection = Faraday.new(url)
    # Faraday retries are not behaving as expected, so we manually do so here.
    i = 0
    begin<commit_msg>crichton -> moya in comment, add db:drop, explicit db manipulations<commit_after>require "moya/version"

module Moya

  # Initialize rails in the development environment.
  def self.initialize_rails!
    require File.dirname(__FILE__) << "/moya/config/environment"
  end

  # Uses Process.spawn to setup and spin up the moya service rails server
  def self.spawn_rails_process!(port = 3000, initializer_directory = nil)
    rails_root = File.dirname(__FILE__) << '/moya'
    env_vars = env_vars_hash(port, initializer_directory)

    Dir.chdir(rails_root) do
      [ 'bundle exec rake config',
        'bundle exec rake db:drop',
        'bundle exec rake db:create',
        'bundle exec rake db:migrate',
        'bundle exec rake db:seed'
      ].each { |str| system(env_vars, str)}
    end

    @pid = Process.spawn(env_vars, "bundle exec rails server --port #{port}", chdir: "#{rails_root}")
    wait_for_response!("http://localhost:#{port}")

  ensure
    return @pid
  end

  private

  # Waits up to 30 seconds for a response to root from the passed in url
  def self.wait_for_response!(url)
    connection = Faraday.new(url)
    # Faraday retries are not behaving as expected, so we manually do so here.
    i = 0
    begin
{'fraction_non_alphanumeric': 0.0985178727114211, 'fraction_numerical': 0.006102877070619006, 'mean_word_length': 3.526627218934911, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 4, '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': '5161802', 'n_tokens_mistral': 747, 'n_tokens_neox': 702, 'n_words': 228}