为什么 Rails 不会在每个请求时刷新类(尽管有配置)?
最近,每次更改代码时,我都必须重新启动开发服务器。我的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
所以事实证明,
config.threadsafe!
覆盖了config.cache_classes = false
的效果,尽管它实际上并没有覆盖值cache_classes
(请参阅我的问题以获取证明)。深入研究 Rails 源代码可能会阐明为什么会出现这种情况,但实际上我的开发环境中并不需要线程安全行为。相反,我将 environment.rb 中对 config.threadsafe! 的调用替换为,现在一切正常。
So it turns out that
config.threadsafe!
overwrites the effect ofconfig.cache_classes = false
, even though it doesn't actually overwrite the value ofcache_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 toconfig.threadsafe!
in environment.rb toand everything works fine now.
如果其他人遇到此问题,解决方案是以下顺序:
config.threadsafe!
必须位于config.cache_classes
之前。像这样重新排序来修复它:答案来自:Rails:cache_classes => ; false 仍然缓存
If anyone else has this problem the solution was the order:
config.threadsafe!
has to come beforeconfig.cache_classes
. Reorder it like this to fix it:answer from: Rails: cache_classes => false still caches
我怀疑您期望刷新的类在您的配置中的某个位置已被“必需”。请注意,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
require
s 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尽管线程安全!解决方案有效,我还想指出为了您的利益以及以下内容之后可能出现的其他内容...
如果您正在编辑直接位于供应商/引擎目录中的引擎代码,则这些文件将不会在没有更新的情况下更新重新启动。可能有一个配置选项来启用此类功能。但是,如果您使用引擎将大量功能从应用程序中分离出来,记住这一点非常重要。
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.
我的猜测是,它不会为每个请求重新加载类,因为它们在请求之间没有更改。因此,系统会在加载类时记下上次修改时间,并且在更改之前不会重新加载它们。
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.