Rails 2.3.5 线程安全!打破了我的迁移

发布于 2024-10-01 01:57:53 字数 560 浏览 0 评论 0原文

我正在使用 jruby-1.5.3 和 Rails 2.3.5 应用程序。我刚刚开始使用以下方法来解决线程安全问题:

config.threadsafe!
config.eager_load_paths << "#{RAILS_ROOT}/lib"

效果很好,但是我注意到在部署到我的临时环境(与生产环境具有相同的配置)时,我得到了未定义的常量。例如,向角色表添加另一个角色的迁移:

class AddSuperAdminRole < ActiveRecord::Migration
  def self.up
    Role.create :rolename => 'super_admin'
  end
end

抛出:

uninitialized constant AddSuperAdminRole::Role

它在开发环境中工作正常,因为我没有运行多线程,所以我知道这就是问题所在。我也尝试过急于加载应用程序/模型路径,但这不起作用。如何让迁移在线程安全的情况下运行?

I'm using jruby-1.5.3 with a rails 2.3.5 app. I've just started playing around with thread safety using:

config.threadsafe!
config.eager_load_paths << "#{RAILS_ROOT}/lib"

Which works fine, I just noticed however on deployment to my staging environment (which has the same config as production) that I get undefined constants. For instance, a migration that adds another role to a Role table:

class AddSuperAdminRole < ActiveRecord::Migration
  def self.up
    Role.create :rolename => 'super_admin'
  end
end

throws a:

uninitialized constant AddSuperAdminRole::Role

It works fine in dev environment because i'm not running that multithreaded so I know that's the issue. I've tried eager loading the app/models path also but that didn't work. How do I get migrations running with threadsafety?

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

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

发布评论

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

评论(2

半暖夏伤 2024-10-08 01:57:53

升级到 2.3.10 修复了这个问题。

upgrading to 2.3.10 fixed this.

月下凄凉 2024-10-08 01:57:53

来自票证 #2506。下面我链接了 Rails 中的线程安全方法。您将看到 config.dependency_loading 被设置为 false,因为它不是线程安全的,因此迁移会自动加载其依赖项。

# 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!
      self.preload_frameworks = true
      self.cache_classes = true
      self.dependency_loading = false
      self.action_controller.allow_concurrency = true
      self
    end

以下是 Joshua Peek 在票证评论中对该问题的回应:

我建议需要模型
您在迁移中需要的,或者
更好地包括模型定义
再次,所以迁移不依赖于
关于它的存在。

From ticket #2506 on the Rails Lighthouse site. Below i linked the threadsafe method in Rails. You'll see that config.dependency_loading is set to false because it's not thread safe and therefore the migrations are getting their dependencies auto-loaded.

# 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!
      self.preload_frameworks = true
      self.cache_classes = true
      self.dependency_loading = false
      self.action_controller.allow_concurrency = true
      self
    end

Here's Joshua Peek's response to the problem in comments to the ticket:

I would suggest requiring the models
the you need in the migration, or
better including the model definition
again so the migration doesn't depend
on its existence.

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