SQL 不会在 Phusion Passenger 下的生产环境中执行

发布于 2024-11-19 01:16:06 字数 377 浏览 2 评论 0原文

我有一个类似于以下内容的代码:

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 技术交流群。

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

发布评论

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

评论(2

浮光之海 2024-11-26 01:16:06

我的心灵调试表明你被 Passenger 的分叉模型咬伤了。

基本上,Passenger 从主进程中分叉出一堆工作进程,该主进程已经加载了 Rails 环境。这减少了工作人员初始化所需的时间,因为所有模型、控制器等类都已经存在。

现在,来自乘客文档:

请注意 Phusion Passenger
自动重新建立
连接到数据库时
创建一个新的工作进程,
这就是为什么你通常不会遇到
使用 smart 时出现任何数据库问题
产卵模式。

您的 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:

Note that Phusion Passenger
automatically reestablishes the
connection to the database upon
creating a new worker process, which
is why you normally do not encounter
any database issues when using smart
spawning mode.

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.

壹場煙雨 2024-11-26 01:16:06

检查您的乘客错误日志,这些日志文件可能与标准 Rails 日志文件不同。

check your passenger error logs, these may be different from the standard rails log files.

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