Rails Resque 工人因 PGError 失败:服务器意外关闭了连接 [英] Rails Resque workers fail with PGError: server closed the connection unexpectedly

查看:41
本文介绍了Rails Resque 工人因 PGError 失败:服务器意外关闭了连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Ubuntu 9.10、Rails 2.3.4、ruby-ee 2010.01、PostgreSQL 8.4.2 上有运行 rails 应用程序的站点和以生产模式运行的 resque 工作程序

I have site running rails application and resque workers running in production mode, on Ubuntu 9.10, Rails 2.3.4, ruby-ee 2010.01, PostgreSQL 8.4.2

Workers 不断提出错误:PGError:服务器意外关闭了连接.

Workers constantly raised errors: PGError: server closed the connection unexpectedly.

我最好的猜测是,master resque 进程建立到数据库的连接(例如,authlogic 在使用 User.acts_as_authentic 时会这样做),同时加载 Rails 应用程序类,并且该连接在 fork()ed 进程中(退出时?)被破坏,所以下一个分叉的孩子会得到一种损坏的全局 ActiveRecord::Base.connection

My best guess is that master resque process establishes connection to db (e.g. authlogic does that when use User.acts_as_authentic), while loading rails app classes, and that connection becomes corrupted in fork()ed process (on exit?), so next forked children get kind of broken global ActiveRecord::Base.connection

我可以用这个 示例代码 重现非常相似的行为,在 resque Worker 中模仿 fork/processing.(AFAIK,libpq 的用户建议无论如何在分叉进程中重新创建连接,否则不安全)

I could reproduce very similar behaviour with this sample code imitating fork/processing in resque worker. (AFAIK, users of libpq recommended to recreate connections in forked process anyway, otherwise it's not safe )

但是,奇怪的是,当我使用 pgbouncer 或 pgpool-II 而不是直接 pgsql 连接时,不会出现此类错误.

But, the odd thing is that when I use pgbouncer or pgpool-II instead of direct pgsql connection, such errors do not appear.

所以,问题是我应该在哪里以及如何挖掘以找出为什么它因普通连接而损坏并且正在使用连接池?或者合理的解决方法?

So, the question is where and how should I dig to find out why it is broken for plain connection and is working with connection pools? Or reasonable workaround?

推荐答案

当我创建 Nestor 时,我有同样的问题.解决方案是在分叉过程中重新建立连接.请参阅 http 上的相关代码://github.com/francois/nestor/blob/master/lib/nestor/mappers/rails/test/unit.rb#L162

When I created Nestor, I had the same kind of problem. The solution was to re-establish the connection in the forked process. See the relevant code at http://github.com/francois/nestor/blob/master/lib/nestor/mappers/rails/test/unit.rb#L162

从我对 Resque 代码的非常 有限的观察来看,我认为应该在此处调用 #establish_connection:https://github.com/resque/resque/blob/master/lib/resque/worker.rb#L123

From my very limited look at Resque code, I believe a call to #establish_connection should be done right about here: https://github.com/resque/resque/blob/master/lib/resque/worker.rb#L123

这篇关于Rails Resque 工人因 PGError 失败:服务器意外关闭了连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆