aboutsummaryrefslogtreecommitdiffstats
path: root/railties
diff options
context:
space:
mode:
Diffstat (limited to 'railties')
-rw-r--r--railties/CHANGELOG.md2
-rw-r--r--railties/lib/rails/application.rb7
-rw-r--r--railties/lib/rails/application/bootstrap.rb18
-rw-r--r--railties/lib/rails/application/configuration.rb20
-rw-r--r--railties/lib/rails/application/finisher.rb6
-rw-r--r--railties/lib/rails/commands/server.rb9
-rw-r--r--railties/lib/rails/engine.rb5
-rw-r--r--railties/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt3
-rw-r--r--railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt6
-rw-r--r--railties/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt5
-rw-r--r--railties/lib/rails/railtie.rb3
-rw-r--r--railties/lib/rails/railtie/configuration.rb10
-rw-r--r--railties/test/application/configuration_test.rb36
-rw-r--r--railties/test/application/middleware_test.rb4
-rw-r--r--railties/test/application/rake_test.rb23
-rw-r--r--railties/test/isolation/abstract_unit.rb2
16 files changed, 102 insertions, 57 deletions
diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md
index 851f41249a..fea18b5f47 100644
--- a/railties/CHANGELOG.md
+++ b/railties/CHANGELOG.md
@@ -5,6 +5,8 @@
default_url_options[:script_name] to set proper application's mount point by
yourself. *Piotr Sarnacki*
+* `config.threadsafe!` is deprecated in favor of `config.eager_load` which provides a more fine grained control on what is eager loaded *José Valim*
+
* The migration generator will now produce AddXXXToYYY/RemoveXXXFromYYY migrations with references statements, for instance
rails g migration AddReferencesToProducts user:references supplier:references{polymorphic}
diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb
index 3531728421..ae872f2bb0 100644
--- a/railties/lib/rails/application.rb
+++ b/railties/lib/rails/application.rb
@@ -16,7 +16,7 @@ module Rails
#
# Besides providing the same configuration as Rails::Engine and Rails::Railtie,
# the application object has several specific configurations, for example
- # "allow_concurrency", "cache_classes", "consider_all_requests_local", "filter_parameters",
+ # "cache_classes", "consider_all_requests_local", "filter_parameters",
# "logger" and so forth.
#
# Check Rails::Application::Configuration to see them all.
@@ -216,8 +216,9 @@ module Rails
railties.each { |r| r.run_tasks_blocks(app) }
super
require "rails/tasks"
+ config = self.config
task :environment do
- $rails_rake_task = true
+ config.eager_load = false
require_environment!
end
end
@@ -296,7 +297,7 @@ module Rails
middleware.use ::ActionDispatch::Static, paths["public"].first, config.static_cache_control
end
- middleware.use ::Rack::Lock unless config.allow_concurrency
+ middleware.use ::Rack::Lock unless config.cache_classes
middleware.use ::Rack::Runtime
middleware.use ::Rack::MethodOverride
middleware.use ::ActionDispatch::RequestId
diff --git a/railties/lib/rails/application/bootstrap.rb b/railties/lib/rails/application/bootstrap.rb
index e567df7162..a1bc95550b 100644
--- a/railties/lib/rails/application/bootstrap.rb
+++ b/railties/lib/rails/application/bootstrap.rb
@@ -13,11 +13,18 @@ module Rails
require "active_support/all" unless config.active_support.bare
end
- # Preload all frameworks specified by the Configuration#frameworks.
- # Used by Passenger to ensure everything's loaded before forking and
- # to avoid autoload race conditions in JRuby.
- initializer :preload_frameworks, :group => :all do
- ActiveSupport::Autoload.eager_autoload! if config.preload_frameworks
+ initializer :set_eager_load, :group => :all do
+ if config.eager_load.nil?
+ warn <<-INFO
+config.eager_load is set to nil. Please update your config/environments/*.rb files accordingly:
+
+ * development - set it to false
+ * test - set it to false (unless you use a tool that preloads your test environment)
+ * production - set it to true
+
+INFO
+ config.eager_load = config.cache_classes
+ end
end
# Initialize the logger early in the stack in case we need to log some deprecation.
@@ -60,7 +67,6 @@ module Rails
end
# Sets the dependency loading mechanism.
- # TODO: Remove files from the $" and always use require.
initializer :initialize_dependency_mechanism, :group => :all do
ActiveSupport::Dependencies.mechanism = config.cache_classes ? :require : :load
end
diff --git a/railties/lib/rails/application/configuration.rb b/railties/lib/rails/application/configuration.rb
index 2b36cc9d0d..7f05b2e7e1 100644
--- a/railties/lib/rails/application/configuration.rb
+++ b/railties/lib/rails/application/configuration.rb
@@ -5,11 +5,11 @@ require 'rails/engine/configuration'
module Rails
class Application
class Configuration < ::Rails::Engine::Configuration
- attr_accessor :allow_concurrency, :asset_host, :asset_path, :assets, :autoflush_log,
+ attr_accessor :asset_host, :asset_path, :assets, :autoflush_log,
:cache_classes, :cache_store, :consider_all_requests_local, :console,
- :dependency_loading, :exceptions_app, :file_watcher, :filter_parameters,
+ :eager_load, :exceptions_app, :file_watcher, :filter_parameters,
:force_ssl, :helpers_paths, :logger, :log_formatter, :log_tags,
- :preload_frameworks, :railties_order, :relative_url_root, :secret_token,
+ :railties_order, :relative_url_root, :secret_token,
:serve_static_assets, :ssl_options, :static_cache_control, :session_options,
:time_zone, :reload_classes_only_on_change,
:queue, :queue_consumer
@@ -20,11 +20,9 @@ module Rails
def initialize(*)
super
self.encoding = "utf-8"
- @allow_concurrency = false
@consider_all_requests_local = false
@filter_parameters = []
@helpers_paths = []
- @dependency_loading = true
@serve_static_assets = true
@static_cache_control = nil
@force_ssl = false
@@ -45,6 +43,7 @@ module Rails
@log_formatter = ActiveSupport::Logger::SimpleFormatter.new
@queue = Rails::Queueing::Queue
@queue_consumer = Rails::Queueing::ThreadedConsumer
+ @eager_load = nil
@assets = ActiveSupport::OrderedOptions.new
@assets.enabled = false
@@ -91,15 +90,12 @@ module Rails
end
end
- # Enable threaded mode. Allows concurrent requests to controller actions and
- # multiple database connections. Also disables automatic dependency loading
- # after boot, and disables reloading code on every request, as these are
- # fundamentally incompatible with thread safety.
def threadsafe!
- @preload_frameworks = true
+ ActiveSupport::Deprecation.warn "config.threadsafe! is deprecated. Rails applications " \
+ "behave by default as thread safe in production as long as config.cache_classes and " \
+ "config.eager_load are set to true"
@cache_classes = true
- @dependency_loading = false
- @allow_concurrency = true
+ @eager_load = true
self
end
diff --git a/railties/lib/rails/application/finisher.rb b/railties/lib/rails/application/finisher.rb
index 60aa40b92f..1952a0fc3a 100644
--- a/railties/lib/rails/application/finisher.rb
+++ b/railties/lib/rails/application/finisher.rb
@@ -50,9 +50,9 @@ module Rails
end
initializer :eager_load! do
- if config.cache_classes && !(defined?($rails_rake_task) && $rails_rake_task)
+ if config.eager_load
ActiveSupport.run_load_hooks(:before_eager_load, self)
- eager_load!
+ config.eager_load_namespaces.each(&:eager_load!)
end
end
@@ -91,7 +91,7 @@ module Rails
# Disable dependency loading during request cycle
initializer :disable_dependency_loading do
- if config.cache_classes && !config.dependency_loading
+ if config.eager_load
ActiveSupport::Dependencies.unhook!
end
end
diff --git a/railties/lib/rails/commands/server.rb b/railties/lib/rails/commands/server.rb
index 9ef64da3ef..a684129353 100644
--- a/railties/lib/rails/commands/server.rb
+++ b/railties/lib/rails/commands/server.rb
@@ -87,6 +87,15 @@ module Rails
middlewares = []
middlewares << [Rails::Rack::Debugger] if options[:debugger]
middlewares << [::Rack::ContentLength]
+
+ # FIXME: add Rack::Lock in the case people are using webrick.
+ # This is to remain backwards compatible for those who are
+ # running webrick in production. We should consider removing this
+ # in development.
+ if server.name == 'Rack::Handler::WEBrick'
+ middlewares << [::Rack::Lock]
+ end
+
Hash.new(middlewares)
end
diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb
index 40f35ae5a6..3a5caf9f62 100644
--- a/railties/lib/rails/engine.rb
+++ b/railties/lib/rails/engine.rb
@@ -339,11 +339,16 @@ module Rails
class << self
attr_accessor :called_from, :isolated
+
alias :isolated? :isolated
alias :engine_name :railtie_name
+ delegate :eager_load!, to: :instance
+
def inherited(base)
unless base.abstract_railtie?
+ Rails::Railtie::Configuration.eager_load_namespaces << base
+
base.called_from = begin
# Remove the line number from backtraces making sure we don't leave anything behind
call_stack = caller.map { |p| p.sub(/:\d+.*/, '') }
diff --git a/railties/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt
index 2016eb8b05..122e7e2b34 100644
--- a/railties/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt
+++ b/railties/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt
@@ -6,6 +6,9 @@
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false
+ # Do not eager load code on boot.
+ config.eager_load = false
+
# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
diff --git a/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt
index 80413be0d3..a627636089 100644
--- a/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt
+++ b/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt
@@ -4,6 +4,12 @@
# Code is not reloaded between requests.
config.cache_classes = true
+ # Eager load code on boot. This eager loads most of Rails and
+ # your application in memory, allowing both thread web servers
+ # and those relying on copy on write to perform better.
+ # Rake tasks automatically ignore this option for performance.
+ config.eager_load = true
+
# Full error reports are disabled and caching is turned on.
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
diff --git a/railties/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt
index 878deb49d7..8ab27eb6e1 100644
--- a/railties/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt
+++ b/railties/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt
@@ -7,6 +7,11 @@
# and recreated between test runs. Don't rely on the data there!
config.cache_classes = true
+ # Do not eager load code on boot. This avoids loading your whole application
+ # just for the purpose of running a single test. If you are using a tool that
+ # preloads Rails for running tests, you may have to set it to true.
+ config.eager_load = false
+
# Configure static asset server for tests with Cache-Control for performance.
config.serve_static_assets = true
config.static_cache_control = "public, max-age=3600"
diff --git a/railties/lib/rails/railtie.rb b/railties/lib/rails/railtie.rb
index 1cb99463cc..fba685c769 100644
--- a/railties/lib/rails/railtie.rb
+++ b/railties/lib/rails/railtie.rb
@@ -178,9 +178,6 @@ module Rails
@config ||= Railtie::Configuration.new
end
- def eager_load!
- end
-
def railtie_namespace
@railtie_namespace ||= self.class.parents.detect { |n| n.respond_to?(:railtie_namespace) }
end
diff --git a/railties/lib/rails/railtie/configuration.rb b/railties/lib/rails/railtie/configuration.rb
index 1c6b3769a5..9dc8843887 100644
--- a/railties/lib/rails/railtie/configuration.rb
+++ b/railties/lib/rails/railtie/configuration.rb
@@ -7,6 +7,16 @@ module Rails
@@options ||= {}
end
+ # Expose the eager_load_namespaces at "module" level for convenience.
+ def self.eager_load_namespaces #:nodoc:
+ @@eager_load_namespaces ||= []
+ end
+
+ # All namespaces that are eager loaded
+ def eager_load_namespaces
+ @@eager_load_namespaces ||= []
+ end
+
# Add files that should be watched for change.
def watchable_files
@@watchable_files ||= []
diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb
index c813defe93..ebdbbaee8b 100644
--- a/railties/test/application/configuration_test.rb
+++ b/railties/test/application/configuration_test.rb
@@ -139,22 +139,19 @@ module ApplicationTests
assert_instance_of Pathname, Rails.root
end
- test "marking the application as threadsafe sets the correct config variables" do
+ test "initialize an eager loaded, cache classes app" do
add_to_config <<-RUBY
- config.threadsafe!
+ config.eager_load = true
+ config.cache_classes = true
RUBY
require "#{app_path}/config/application"
- assert AppTemplate::Application.config.allow_concurrency
+ assert AppTemplate::Application.initialize!
end
- test "initialize a threadsafe app" do
- add_to_config <<-RUBY
- config.threadsafe!
- RUBY
-
+ test "application is always added to eager_load namespaces" do
require "#{app_path}/config/application"
- assert AppTemplate::Application.initialize!
+ assert AppTemplate::Application, AppTemplate::Application.config.eager_load_namespaces
end
test "asset_path defaults to nil for application" do
@@ -162,10 +159,11 @@ module ApplicationTests
assert_equal nil, AppTemplate::Application.config.asset_path
end
- test "the application can be marked as threadsafe when there are no frameworks" do
+ test "the application can be eager loaded even when there are no frameworks" do
FileUtils.rm_rf("#{app_path}/config/environments")
add_to_config <<-RUBY
- config.threadsafe!
+ config.eager_load = true
+ config.cache_classes = true
RUBY
use_frameworks []
@@ -175,22 +173,6 @@ module ApplicationTests
end
end
- test "frameworks are not preloaded by default" do
- require "#{app_path}/config/environment"
-
- assert ActionController.autoload?(:Caching)
- end
-
- test "frameworks are preloaded with config.preload_frameworks is set" do
- add_to_config <<-RUBY
- config.preload_frameworks = true
- RUBY
-
- require "#{app_path}/config/environment"
-
- assert !ActionView.autoload?(:AssetPaths)
- end
-
test "filter_parameters should be able to set via config.filter_parameters" do
add_to_config <<-RUBY
config.filter_parameters += [ :foo, 'bar', lambda { |key, value|
diff --git a/railties/test/application/middleware_test.rb b/railties/test/application/middleware_test.rb
index 9c9ed0cd6b..34b460d0a7 100644
--- a/railties/test/application/middleware_test.rb
+++ b/railties/test/application/middleware_test.rb
@@ -87,8 +87,8 @@ module ApplicationTests
assert !middleware.include?("ActiveRecord::QueryCache")
end
- test "removes lock if allow concurrency is set" do
- add_to_config "config.allow_concurrency = true"
+ test "removes lock if cache classes is set" do
+ add_to_config "config.cache_classes = true"
boot!
assert !middleware.include?("Rack::Lock")
end
diff --git a/railties/test/application/rake_test.rb b/railties/test/application/rake_test.rb
index a450f90dbf..b05fe3aed5 100644
--- a/railties/test/application/rake_test.rb
+++ b/railties/test/application/rake_test.rb
@@ -55,6 +55,29 @@ module ApplicationTests
assert_match "Doing something...", output
end
+ def test_does_not_explode_when_accessing_a_model_with_eager_load
+ add_to_config <<-RUBY
+ config.eager_load = true
+
+ rake_tasks do
+ task :do_nothing => :environment do
+ Hello.new.world
+ end
+ end
+ RUBY
+
+ app_file "app/models/hello.rb", <<-RUBY
+ class Hello
+ def world
+ puts "Hello world"
+ end
+ end
+ RUBY
+
+ output = Dir.chdir(app_path){ `rake do_nothing` }
+ assert_match "Hello world", output
+ end
+
def test_code_statistics_sanity
assert_match "Code LOC: 5 Test LOC: 0 Code to Test Ratio: 1:0.0",
Dir.chdir(app_path){ `rake stats` }
diff --git a/railties/test/isolation/abstract_unit.rb b/railties/test/isolation/abstract_unit.rb
index 8f04692aef..d89cfba56d 100644
--- a/railties/test/isolation/abstract_unit.rb
+++ b/railties/test/isolation/abstract_unit.rb
@@ -12,7 +12,6 @@ require 'bundler/setup'
require 'minitest/autorun'
require 'active_support/test_case'
-# TODO: Remove setting this magic constant
RAILS_FRAMEWORK_ROOT = File.expand_path("#{File.dirname(__FILE__)}/../../..")
# These files do not require any others and are needed
@@ -118,6 +117,7 @@ module TestHelpers
end
add_to_config <<-RUBY
+ config.eager_load = false
config.secret_token = "3b7cd727ee24e8444053437c36cc66c4"
config.session_store :cookie_store, :key => "_myapp_session"
config.active_support.deprecation = :log