diff --git a/lib/spring/application.rb b/lib/spring/application.rb index b64b6b89..b7d6de75 100644 --- a/lib/spring/application.rb +++ b/lib/spring/application.rb @@ -116,6 +116,17 @@ def preload require Spring.application_root_path.join("config", "environment") + # Run user-supplied post-environment-load callbacks (e.g. preloading the + # test runtime so forked children skip 2+s of require chains). + Spring.after_environment_load_callbacks.each do |callback| + begin + callback.call + rescue Exception => err + log "after_environment_load callback raised: #{err.class}: #{err.message}" + raise + end + end + disconnect_database @preloaded = :success diff --git a/lib/spring/configuration.rb b/lib/spring/configuration.rb index cb10abd3..24cf3bbc 100644 --- a/lib/spring/configuration.rb +++ b/lib/spring/configuration.rb @@ -35,6 +35,18 @@ def after_fork(&block) after_fork_callbacks << block end + # Callbacks invoked once on the spring server, after config/environment + # has finished loading and Rails autoloads are wired up. Use this to + # preload heavy test/dev support files that depend on Rails being ready, + # so the forked child doesn't pay the cost on every command. + def after_environment_load_callbacks + @after_environment_load_callbacks ||= [] + end + + def after_environment_load(&block) + after_environment_load_callbacks << block + end + def spawn_on_env @spawn_on_env ||= [] end