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

问题描述

我的网站上运行Rails应用程序和resque工人在生产模式下运行,在Ubuntu 9.10,Rails的2.3.4,红宝石EE 2010.01和PostgreSQL 8.4.2

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

工人不断出现的错误:PGError:服务器意外关闭了连接。

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

我最好的猜测是,主resque过程建立连接到数据库(如authlogic确实,当使用User.acts_as_authentic),同时加载Rails应用程序类,并就此成为叉损坏()的方法(在退出?)所以接下来的分叉孩子得到一种打破全球的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

我可以用这个样品code 模仿叉/在resque工人处理重现非常类似的行为。 (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?

推荐答案

当我创建内斯特,我有同样的问题。解决的办法是重新建立在两岔进程的连接。请参阅相关的code处的

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 code,我相信,打电话给#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:服务器意外关闭了连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!