为什么 Rails 不会在每个请求时刷新类(尽管有配置)?

发布于 2024-12-09 13:02:32 字数 987 浏览 4 评论 0原文

最近,每次更改代码时,我都必须重新启动开发服务器。我的development.rb 文件仍然有这一行:

config.cache_classes = false

我尝试使用调试器验证该值是否已保留。为此,我将配置设置为environment.rb中的全局变量:

$my_initializer = Rails::Initializer.run do |config|
  ...
end

然后我在其中一个控制器中放置了一个debugger行,这样我就可以执行以下操作:

(rdb:2) $my_initializer.configuration.cache_classes
false

这样就消除了<的值的可能性code>cache_classes 在其他地方被设置为 true 。我尝试过使用 Mongrel 和 WEBrick,但它仍然发生。

还有什么可能导致 Rails 不针对每个请求重新加载我的代码?

我正在运行: 杂种1.1.5
WEBrick 1.3.1
Rails 2.3.8
红宝石 1.8.7 p253

编辑: 根据 @Daemin 的建议,我检查了当我将文件保存在文本编辑器(Textmate)中时,文件的 mtime 实际上正在更新

merced:controllers lance$ ls -l people_controller.rb 
-rwxr-xr-x  1 lance  staff  2153 Act 10 18:01 people_controller.rb

然后我进行了更改并保存了文件:

merced:controllers lance$ ls -l people_controller.rb 
-rwxr-xr-x@ 1 lance  staff  2163 Oct 11 12:03 people_controller.rb

所以这不是 mtime 的问题。

I recently started having to restart my development server every time I change my code. My development.rb file still has this line:

config.cache_classes = false

I tried using the debugger verify that this value has stuck around. To do this I set my configuration to a global variable in environment.rb:

$my_initializer = Rails::Initializer.run do |config|
  ...
end

then I put a debugger line in one of my controllers so I could do this:

(rdb:2) $my_initializer.configuration.cache_classes
false

So that eliminated the possibility that the value of cache_classes was getting set to true somewhere else. I've tried using both Mongrel and WEBrick and it still happens.

What else might be causing Rails not to reload my code with every request?

I am running:
Mongrel 1.1.5
WEBrick 1.3.1
Rails 2.3.8
Ruby 1.8.7 p253

EDIT:
at @Daemin 's suggestion I checked that the mtime of my files are are actually getting updated when I save them in my text editor (Textmate)

merced:controllers lance$ ls -l people_controller.rb 
-rwxr-xr-x  1 lance  staff  2153 Act 10 18:01 people_controller.rb

Then I made a change and saved the file:

merced:controllers lance$ ls -l people_controller.rb 
-rwxr-xr-x@ 1 lance  staff  2163 Oct 11 12:03 people_controller.rb

So it's not a problem with the mtimes.

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

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

发布评论

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

评论(5

陈甜 2024-12-16 13:02:32

所以事实证明,config.threadsafe! 覆盖了 config.cache_classes = false 的效果,尽管它实际上并没有覆盖 cache_classes(请参阅我的问题以获取证明)。深入研究 Rails 源代码可能会阐明为什么会出现这种情况,但实际上我的开发环境中并不需要线程安全行为。相反,我将 environment.rb 中对 config.threadsafe! 的调用替换为

config.threadsafe! unless RAILS_ENV == "development"

,现在一切正常。

So it turns out that config.threadsafe! overwrites the effect of config.cache_classes = false, even though it doesn't actually overwrite the value of cache_classes (see my question for proof). Digging around a bit more in the Rails source code might illuminate why this might be, but I don't actually need threadsafe behavior in my development environment. Instead, I replaced my call to config.threadsafe! in environment.rb to

config.threadsafe! unless RAILS_ENV == "development"

and everything works fine now.

究竟谁懂我的在乎 2024-12-16 13:02:32

如果其他人遇到此问题,解决方案是以下顺序:config.threadsafe! 必须位于 config.cache_classes 之前。像这样重新排序来修复它:

...
config.threadsafe!
config.cache_classes = false
...

答案来自:Rails:cache_classes => ; false 仍然缓存

If anyone else has this problem the solution was the order: config.threadsafe! has to come before config.cache_classes. Reorder it like this to fix it:

...
config.threadsafe!
config.cache_classes = false
...

answer from: Rails: cache_classes => false still caches

情归归情 2024-12-16 13:02:32

我怀疑您期望刷新的类在您的配置中的某个位置已被“必需”。请注意,Rails 的依赖项加载发生在 Ruby 的 require 发生之后。如果已经需要某个特定的模块或类,Rails 的依赖加载器将不会处理它,因此不会重新加载它。有关详细说明,请查看这篇文章:http://spacevatican.org/2008 /9/28/是否需要

I suspect that the classes you are expecting to refresh have been 'required' somewhere in your configuration. Note that Rails' dependency loading happens after Ruby's requires have happened. If a particular module or class has already been required, it will not be handled by Rails' dependency loader, and thus it will not be reloaded. For a detailed explanation, check out this article: http://spacevatican.org/2008/9/28/required-or-not

长伴 2024-12-16 13:02:32

尽管线程安全!解决方案有效,我还想指出为了您的利益以及以下内容之后可能出现的其他内容...

如果您正在编辑直接位于供应商/引擎目录中的引擎代码,则这些文件将不会在没有更新的情况下更新重新启动。可能有一个配置选项来启用此类功能。但是,如果您使用引擎将大量功能从应用程序中分离出来,记住这一点非常重要。

Despite the fact that the threadsafe! solution works, I also wanted to point out for your benefit and the others that may come in after the following...

If you're editing engine code that is directly in your vendor/engines directory, those files will not be updated without a restart. There may be a configuration option to enable such functionality. However, this is very important to remember if you have used engines to separate large bits of functionality from your application.

月光色 2024-12-16 13:02:32

我的猜测是,它不会为每个请求重新加载类,因为它们在请求之间没有更改。因此,系统会在加载类时记下上次修改时间,并且在更改之前不会重新加载它们。

My guess would be that it's not reloading the classes for each request because they haven't changed between requests. So the system would note down the last modified time when the classes are loaded, and not reload them until that changed.

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