SQL 不会在 Phusion Passenger 下的生产环境中执行
我有一个类似于以下内容的代码:
class TheClass < ActiveRecord::Base
connection.execute "set language 'us_english'"
// the rest of the code
end
在 Passenger 下的生产中不会执行执行命令!使用 Mongrel 就可以了,将代码放入稍后调用的函数中即可,乘客+开发设置也可以。
Passenger 处理连接或类实例化的方式是否有我不知道的不同?
编辑:
很明显,这是乘客建立连接的方式。因此,问题归结为“在建立连接时是否有受支持的方式来执行 SQL 语句?”
I have a code similar to the following:
class TheClass < ActiveRecord::Base
connection.execute "set language 'us_english'"
// the rest of the code
end
The execute command doesn't get executed in production under Passenger! Using Mongrel it's ok, putting the code inside a later called function works, Passenger+development settings also works.
Is there something different in the way Passenger deals with the connection or the Class instanciation that I'm not aware?
Edit:
It's clear that it's the way Passenger makes connections. So the question boils down to "Is there a supported way to execute a SQL statement when a connection is stablished?"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的心灵调试表明你被 Passenger 的分叉模型咬伤了。
基本上,Passenger 从主进程中分叉出一堆工作进程,该主进程已经加载了 Rails 环境。这减少了工作人员初始化所需的时间,因为所有模型、控制器等类都已经存在。
现在,来自乘客文档:
您的 connection.execute 语句在加载 TheClass 时运行。这实际上只发生一次,在分叉所有工作线程的父进程中。每个工作人员都建立自己的连接,但永远不会运行您的“设置语言”查询,因为 TheClass 已经加载。它在开发模式下工作,因为每次都会重新加载您的类。
您可能可以在 application.rb 中进行某种设置来告诉 ActiveRecord 您的连接语言,否则您可能必须在 before_filter 或其他东西中运行该查询进行丑陋的修改。
My psychic debugging says you're being bitten by Passenger's forking model.
Basically, Passenger forks a bunch of worker processes off a main process, which already has your rails environment loaded. This reduces the amount of time it takes the workers to initialize, since all your model, controller etc. classes are already there.
Now, from the Passenger documentation:
Your connection.execute statement runs when the TheClass is loaded. This actually only happens once, in the parent process that's forking all the workers. The workers each establish their own connections, but never run your 'set language' query since TheClass is already loaded. It works in dev mode because that reloads your classes every time.
There might be some sort of setup you can do in application.rb to tell ActiveRecord your connection language, otherwise you'll probably have to do an ugly hack of running that query in a before_filter or something.
检查您的乘客错误日志,这些日志文件可能与标准 Rails 日志文件不同。
check your passenger error logs, these may be different from the standard rails log files.