Spork.prefork 正在加载 app/models/*

发布于 2024-10-17 06:39:30 字数 6254 浏览 2 评论 0原文

我不知道如何让 spork 不加载我的所有应用程序模型。由于我无法使用 spork 来提供帮助,因此测试模型更改的速度大大减慢。这是我调试 spork 正在加载的内容时得到的结果:

    - Spork Diagnosis -
    -- Summary --
    app/models/account.rb
    app/models/admin.rb
    app/models/affiliate.rb
    app/models/app.rb
    app/models/application_server.rb
    app/models/domain_record.rb
    app/models/domain_zone.rb
    app/models/event.rb
    app/models/oid.rb
    app/models/user.rb
    config/application.rb
    config/boot.rb
    config/environment.rb
    config/environments/test.rb
    config/initializers/api_conversions.rb
    config/initializers/backtrace_silencers.rb
    config/initializers/compass.rb
    config/initializers/devise.rb
    config/initializers/hoptoad.rb
    config/initializers/inflections.rb
    config/initializers/mime_types.rb
    config/initializers/rspec_generator.rb
    config/initializers/secret_token.rb
    config/initializers/session_store.rb
    config/routes.rb
    lib/application_server_api.rb
    lib/oid_helper.rb
    lib/soa_record.rb
    spec/spec_helper.rb



    -- Detail --



    --- app/models/account.rb ---
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `block in require'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:227:in `load_dependency'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `require'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:346:in `require_or_load'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:300:in `depend_on'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:216:in `require_dependency'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/mongoid-2.0.0.rc.7/lib/rails/mongoid.rb:54:in `load_model'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/mongoid-2.0.0.rc.7/lib/rails/mongoid.rb:17:in `block (2 levels) in load_models'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/mongoid-2.0.0.rc.7/lib/rails/mongoid.rb:16:in `each'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/mongoid-2.0.0.rc.7/lib/rails/mongoid.rb:16:in `block in load_models'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/paths.rb:102:in `block in each'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/paths.rb:102:in `each'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/paths.rb:102:in `each'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/mongoid-2.0.0.rc.7/lib/rails/mongoid.rb:15:in `load_models'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/mongoid-2.0.0.rc.7/lib/mongoid/railtie.rb:88:in `block (2 levels) in '
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:419:in `_run_prepare_callbacks'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/actionpack-3.0.3/lib/action_dispatch/middleware/callbacks.rb:40:in `initialize'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/actionpack-3.0.3/lib/action_dispatch/middleware/stack.rb:33:in `new'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/actionpack-3.0.3/lib/action_dispatch/middleware/stack.rb:33:in `build'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/actionpack-3.0.3/lib/action_dispatch/middleware/stack.rb:79:in `block in build'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/actionpack-3.0.3/lib/action_dispatch/middleware/stack.rb:79:in `each'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/actionpack-3.0.3/lib/action_dispatch/middleware/stack.rb:79:in `inject'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/actionpack-3.0.3/lib/action_dispatch/middleware/stack.rb:79:in `build'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application.rb:162:in `app'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application/finisher.rb:35:in `block in '
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/initializable.rb:25:in `instance_exec'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/initializable.rb:25:in `run'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/initializable.rb:50:in `block in run_initializers'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/initializable.rb:49:in `each'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/initializable.rb:49:in `run_initializers'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application.rb:134:in `initialize!'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application.rb:77:in `method_missing'
    config/environment.rb:8:in `'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `block in require'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:225:in `block in load_dependency'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:596:in `new_constants_in'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:225:in `load_dependency'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `require'
    spec/spec_helper.rb:9:in `block in '
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/spork-0.9.0.rc3/lib/spork.rb:23:in `prefork'
    spec/spec_helper.rb:4:in `'

更新:我的 spec_helper.rb

require 'rubygems'
require 'spork'

ENV["RAILS_ENV"] = "test"

Spork.prefork do
  require File.expand_path(File.dirname(__FILE__) + '/../config/environment')
  require 'rspec/rails'
end

Spork.each_run do
  # Hub::Application.reload_routes!
end

Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}

如果我移动 require File.expand_path(File.dirname(__FILE__) + '/../config /environment') 在预分叉之外,我的模型没有预加载,但正如您可能期望的那样,运行我的测试需要更长的时间。

关于如何让 spork 不在 Rails 3 应用程序上加载模型有什么想法吗?我尝试过使用 spork 0.8.4、master 分支,目前使用 0.9.0.rc3。有谁知道如何避免预加载模型?

I can not figure out how to get spork not to load all of my app models. Testing changes to my models is greatly slowed down as I am unable to use spork to help. This is what I get when I debug what spork is loading:

    - Spork Diagnosis -
    -- Summary --
    app/models/account.rb
    app/models/admin.rb
    app/models/affiliate.rb
    app/models/app.rb
    app/models/application_server.rb
    app/models/domain_record.rb
    app/models/domain_zone.rb
    app/models/event.rb
    app/models/oid.rb
    app/models/user.rb
    config/application.rb
    config/boot.rb
    config/environment.rb
    config/environments/test.rb
    config/initializers/api_conversions.rb
    config/initializers/backtrace_silencers.rb
    config/initializers/compass.rb
    config/initializers/devise.rb
    config/initializers/hoptoad.rb
    config/initializers/inflections.rb
    config/initializers/mime_types.rb
    config/initializers/rspec_generator.rb
    config/initializers/secret_token.rb
    config/initializers/session_store.rb
    config/routes.rb
    lib/application_server_api.rb
    lib/oid_helper.rb
    lib/soa_record.rb
    spec/spec_helper.rb



    -- Detail --



    --- app/models/account.rb ---
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `block in require'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:227:in `load_dependency'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `require'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:346:in `require_or_load'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:300:in `depend_on'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:216:in `require_dependency'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/mongoid-2.0.0.rc.7/lib/rails/mongoid.rb:54:in `load_model'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/mongoid-2.0.0.rc.7/lib/rails/mongoid.rb:17:in `block (2 levels) in load_models'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/mongoid-2.0.0.rc.7/lib/rails/mongoid.rb:16:in `each'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/mongoid-2.0.0.rc.7/lib/rails/mongoid.rb:16:in `block in load_models'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/paths.rb:102:in `block in each'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/paths.rb:102:in `each'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/paths.rb:102:in `each'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/mongoid-2.0.0.rc.7/lib/rails/mongoid.rb:15:in `load_models'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/mongoid-2.0.0.rc.7/lib/mongoid/railtie.rb:88:in `block (2 levels) in '
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:419:in `_run_prepare_callbacks'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/actionpack-3.0.3/lib/action_dispatch/middleware/callbacks.rb:40:in `initialize'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/actionpack-3.0.3/lib/action_dispatch/middleware/stack.rb:33:in `new'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/actionpack-3.0.3/lib/action_dispatch/middleware/stack.rb:33:in `build'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/actionpack-3.0.3/lib/action_dispatch/middleware/stack.rb:79:in `block in build'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/actionpack-3.0.3/lib/action_dispatch/middleware/stack.rb:79:in `each'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/actionpack-3.0.3/lib/action_dispatch/middleware/stack.rb:79:in `inject'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/actionpack-3.0.3/lib/action_dispatch/middleware/stack.rb:79:in `build'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application.rb:162:in `app'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application/finisher.rb:35:in `block in '
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/initializable.rb:25:in `instance_exec'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/initializable.rb:25:in `run'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/initializable.rb:50:in `block in run_initializers'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/initializable.rb:49:in `each'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/initializable.rb:49:in `run_initializers'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application.rb:134:in `initialize!'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application.rb:77:in `method_missing'
    config/environment.rb:8:in `'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `block in require'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:225:in `block in load_dependency'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:596:in `new_constants_in'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:225:in `load_dependency'
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `require'
    spec/spec_helper.rb:9:in `block in '
    /Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/spork-0.9.0.rc3/lib/spork.rb:23:in `prefork'
    spec/spec_helper.rb:4:in `'

update: my spec_helper.rb

require 'rubygems'
require 'spork'

ENV["RAILS_ENV"] = "test"

Spork.prefork do
  require File.expand_path(File.dirname(__FILE__) + '/../config/environment')
  require 'rspec/rails'
end

Spork.each_run do
  # Hub::Application.reload_routes!
end

Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}

If I move require File.expand_path(File.dirname(__FILE__) + '/../config/environment') out of the prefork, my models are not preloaded, but as you might expect it takes much longer to run my tests.

Any ideas on how to get spork to not load models on a Rails 3 app? I have tried using spork 0.8.4, master branch, and currently 0.9.0.rc3. Does anyone have any ideas how I can avoid having the models preloaded?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眼眸 2024-10-24 06:39:30

经过很长时间的旋转,我终于得到了一些可以接受的东西:

Spork.prefork do
  require "rails/mongoid"
  Spork.trap_class_method(Rails::Mongoid, :load_models)

  # The following does not work correctly with Devise's routes that load the User model. 
  # Results in NameError unintitialized *
  # :reload_routes! triggers :devise_for which loads and caches the User class.
  # https://github.com/timcharper/spork/wiki/Spork.trap_method-Jujutsu
  # require "rails/application"
  # Spork.trap_method(Rails::Application, :reload_routes!) 

  require 'factory_girl_rails'
  Spork.trap_class_method(Factory, :find_definitions)

  require File.expand_path(File.dirname(__FILE__) + '/../config/environment')

end

问题主要是由于 Rails::Mongoid#load_models 造成的。在检查了 spork 的调试输出和加载方式的回溯之后,提供了一些关于加载模型的线索。此页面有更多详细信息 https://github.com/timcharper/spork /wiki/Spork.trap_method-Jujutsu,但我没有找到:reload_routes!帮助 Devise 强制预加载 User 类。

我的规格现在运行得更快了。如果我能让我的用户模型不被缓存,我就会很幸福,但在那之前,我会感到非常满意。

After much time spinning my wheels I finally have something that is acceptable:

Spork.prefork do
  require "rails/mongoid"
  Spork.trap_class_method(Rails::Mongoid, :load_models)

  # The following does not work correctly with Devise's routes that load the User model. 
  # Results in NameError unintitialized *
  # :reload_routes! triggers :devise_for which loads and caches the User class.
  # https://github.com/timcharper/spork/wiki/Spork.trap_method-Jujutsu
  # require "rails/application"
  # Spork.trap_method(Rails::Application, :reload_routes!) 

  require 'factory_girl_rails'
  Spork.trap_class_method(Factory, :find_definitions)

  require File.expand_path(File.dirname(__FILE__) + '/../config/environment')

end

The problem was mostly due to Rails::Mongoid#load_models. After inspecting the debug output of spork and the backtrace of how things are loaded, some clues are provided as to what is loading the models. This page goes into a bit more detail https://github.com/timcharper/spork/wiki/Spork.trap_method-Jujutsu, but I do not find the :reload_routes! helping with Devise forcing the User class to be preloaded.

My specs are running much faster now. If only I could get my User model to not be cached I would be in bliss, but until then, I shall be mostly satisfied.

千纸鹤带着心事 2024-10-24 06:39:30

我在 RSpec 和 Cucumber on Rails 3.1 中都有以下设置,并且模型令人耳目一新:

Setfactory_girls_rails require =>; Gemfile 中的 false

gem 'factory_girl_rails', :require => false

将 Spork.prefork 和 Spork.each_run 块替换为以下代码。

if Spork.using_spork?

  ActiveSupport::Dependencies.clear
  ActiveRecord::Base.instantiate_observers

  Spork.prefork do

  end

  Spork.each_run do
    require 'factory_girl_rails'

    # reload all the models
    Dir["#{Rails.root}/app/models/**/*.rb"].each do |model|
      load model
    end

    # reload routes
    Rails.application.reload_routes!

  end

end

I have the following setup in both RSpec and Cucumber on Rails 3.1 and models are refreshing:

Set factory_girls_rails require => false in your Gemfile

gem 'factory_girl_rails', :require => false

Replace the Spork.prefork and Spork.each_run blocks with the following code.

if Spork.using_spork?

  ActiveSupport::Dependencies.clear
  ActiveRecord::Base.instantiate_observers

  Spork.prefork do

  end

  Spork.each_run do
    require 'factory_girl_rails'

    # reload all the models
    Dir["#{Rails.root}/app/models/**/*.rb"].each do |model|
      load model
    end

    # reload routes
    Rails.application.reload_routes!

  end

end
失眠症患者 2024-10-24 06:39:30

以下建议的解决方案此处对我有用。它还会重新加载用户模型。

# env.rb
Spork.each_run do
  require 'factory_girl_rails'
  # reload all the models
  Dir["#{Rails.root}/app/models/**/*.rb"].each do |model|
    load model
  end
end

The following solution suggested here works for me. It also reloads the User model.

# env.rb
Spork.each_run do
  require 'factory_girl_rails'
  # reload all the models
  Dir["#{Rails.root}/app/models/**/*.rb"].each do |model|
    load model
  end
end
书间行客 2024-10-24 06:39:30

这个解决方案对我有用(来自 http://my .rails-royce.org/2011/03/17/rspec-2-and-fixtures/

Spork.prefork do
  require 'rails/application'
  Spork.trap_method(Rails::Application, :reload_routes!)
  Spork.trap_method(Rails::Application::RoutesReloader, :reload!) 
  Spork.trap_method(Rails::Application, :eager_load!)
  require File.expand_path("../../config/environment", __FILE__)
  Rails.application.railties.all { |r| r.eager_load! }

  require 'rspec/rails'

  RSpec.configure do |config|
     ...
  end
end

请注意,在each_run中执行“加载模型”只是部分解决方案,这将重新评估模型代码,但例如删除模型方法后,它仍将存在于内存中,直到您重新启动 Spork。

This solution works for me (from http://my.rails-royce.org/2011/03/17/rspec-2-and-fixtures/)

Spork.prefork do
  require 'rails/application'
  Spork.trap_method(Rails::Application, :reload_routes!)
  Spork.trap_method(Rails::Application::RoutesReloader, :reload!) 
  Spork.trap_method(Rails::Application, :eager_load!)
  require File.expand_path("../../config/environment", __FILE__)
  Rails.application.railties.all { |r| r.eager_load! }

  require 'rspec/rails'

  RSpec.configure do |config|
     ...
  end
end

Note that doing a "load model" in each_run is only a partial solution, this will re-evaluate the model code, but for instance after removing a model method it will still exist in memory until you restart Spork.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文