【问题标题】:Unicorn! and PostgreSQL独角兽!和 PostgreSQL
【发布时间】:2012-01-05 23:05:03
【问题描述】:

我正在将旧的 Rails 和 PostgreSQL 应用从 2.1 升级到 Rails 3(通过成功的 2.3.11 中间步骤)。

最后,当我让所有 Rspec 顺利运行时,我安装了 Mongrel 1.2.0.pre2 并启动并运行 - 一切正常。甚至 100 个并发用户的 JMeter 测试用例也通过了。

但是当我对 Unicorn 进行同样的尝试时!服务器,当我自己使用它时它工作正常,但是当我用 JMeter 加载它时,它从 JMeter 尝试发布登录的那一刻开始给出 100% 的错误。同时,当我检查某些特定页面时,它给了我这样的奇怪错误:

undefined method `eq' for nil:NilClass

在终端我得到以下输出(但只有几次):

message type 0x43 arrived from server while idle
message type 0x5a arrived from server while idle
WARNING:  there is already a transaction in progress

编辑:这似乎是共享 PGconnect 对象的问题,所以我删除了一些以前的文本

我发现PostgreSQL docs 指定PGconn 对象不能在发出并发请求的线程之间共享。

是独角兽!还是 AR 在同一个 PGconn 对象中混合来自不同线程的请求?

数据库.yml:

production:
  adapter: postgresql
  encoding: unicode
  database: ***
  username: ***
  password: ***
  host: 127.0.0.1

我什至试图添加这个无济于事:

  allow_concurrency: true

unicorn.conf:

worker_processes 8
working_directory "/full/path/to/app"
listen '/tmp/app.sock', :backlog => 512
timeout 30
pid "/full/path/to/app/tmp/pids/puppetmaster_unicorn.pid"

preload_app true
  if GC.respond_to?(:copy_on_write_friendly=)
  GC.copy_on_write_friendly = true
end

规格:

  • Rails 3.0.6(由于部署环境)
  • 独角兽! 4.1.1
  • Nginx 1.0.5
  • pg gem 0.11.0
  • PostgreSQL 9.0.4
  • Mac OS X 10.7.2

如果 Rails 版本应该是罪魁祸首,我当然可以升级到最新版本。我刚刚开始了解服务提供商拥有的东西。

【问题讨论】:

  • 你在 unicorn.rb 中创建了多少个独角兽工人?什么超时?
  • 我用 unicorn.conf 更新了我的问题

标签: ruby-on-rails-3 postgresql unicorn


【解决方案1】:

不要在进程之间共享数据库连接,这一点很重要。当您使用 preload_app 运行 Rails 时,Rails 将在 unicorn master 分叉 worker 之前建立 AR 连接。示例 unicorn.conf.rb 建议在 before_fork 挂钩中断开 AR 连接。 AR 将自动重新建立一个新的连接,因为每个工人都需要它后分叉。

摘自http://unicorn.bogomips.org/examples/unicorn.conf.rb:

before_fork do |server, worker|
  # the following is highly recomended for Rails + "preload_app true"
  # as there's no need for the master process to hold a connection
  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.connection.disconnect!
  # ...
end

【讨论】:

  • 是的,在 Unicorn 邮件列表的帮助下,我也发现了这一点,但忘记更新我的答案。我奖励你为我做这件事。谢谢。
猜你喜欢
  • 2014-01-15
  • 2013-01-10
  • 2012-09-11
  • 1970-01-01
  • 2015-12-01
  • 2013-02-20
  • 1970-01-01
  • 1970-01-01
  • 2011-01-30
相关资源
最近更新 更多