Module: CarrotRpc::ServerRunner::AutoloadRails

Defined in:
lib/carrot_rpc/server_runner/autoload_rails.rb

Overview

Loads the Rails application so that the servers can use the Rails gems and environment.

Class Method Summary collapse

Class Method Details

.conditionally_load_root(root, logger:) ⇒ true

Loads Rails app at ‘root` if `CarrotRpc.configuration.autoload_rails`

Parameters:

  • root (String)

    path to the root of the Rails app

  • logger (Logger)

    logger to print success to.

Returns:

  • (true)

Raises:

  • (LoadError)

    if rails cannot be loaded



36
37
38
39
40
# File 'lib/carrot_rpc/server_runner/autoload_rails.rb', line 36

def self.conditionally_load_root(root, logger:)
  if CarrotRpc.configuration.autoload_rails
    load_root(root, logger: logger)
  end
end

.environment_path(root) ⇒ String

Path to the ‘config/environment.rb`, which is the file that must actually be `require`d to load Rails.

Parameters:

  • root (String)

    path to the root of the Rails app

Returns:

  • (String)


7
8
9
# File 'lib/carrot_rpc/server_runner/autoload_rails.rb', line 7

def self.environment_path(root)
  File.join(root, "config/environment.rb")
end

.load_root(root, logger:) ⇒ true

Attempts to load Rails app at ‘root`.

Parameters:

  • root (String)

    path to the root of the Rails app

  • logger (Logger)

    logger to print success to.

Returns:

  • (true)

Raises:

  • (LoadError)

    if rails cannot be loaded



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/carrot_rpc/server_runner/autoload_rails.rb', line 17

def self.load_root(root, logger:)
  rails_path = environment_path(root)

  if File.exist?(rails_path)
    logger.info "Rails app found at: #{rails_path}"
    set_rack_rails_env
    require rails_path
    ::Rails.application.eager_load!
    true
  else
    require rails_path
  end
end

.set_rack_rails_envVoid

Set Rails/Rack env vars to test when server test mode is enabled.

Returns:

  • (Void)


45
46
47
48
49
50
51
52
53
# File 'lib/carrot_rpc/server_runner/autoload_rails.rb', line 45

def self.set_rack_rails_env
  if CarrotRpc.configuration.server_test_mode
    env_mode = "test"
    ENV["RACK_ENV"] = ENV["RAILS_ENV"] = env_mode
  else
    env_mode = "development"
    ENV["RACK_ENV"] ||= ENV["RAILS_ENV"] ||= env_mode
  end
end