Resque 工作线程因 PostgreSQL 服务器而失败
-
26-10-2019 - |
题
我已经成功地设置了我的工作人员,他们过去可以毫无问题地执行(在开发中),但现在他们在生产或开发中都没有(我猜是从 SQlite3 更改为 PostgreSQL 后)。
当我运行 rake 命令来运行工作人员时 rake resque:work QUEUE=*
我收到以下错误和堆栈跟踪:
getaddrinfo: nodename nor servname provided, or not known
运行时出现以下错误 heroku rake resque:work QUEUE=*
在控制台中测试队列中待处理的工作人员。
Class SentimentJob
Arguments [4, 5, 6]
Exception ActiveRecord::StatementInvalid
Error PGError: server closed the connection unexpectedly This probably means
the server terminated abnormally before or while processing the request.
: SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc,
a.attnotnull FROM pg_attribute a LEFT JOIN pg_attrdef d ON a.attrelid =
d.adrelid AND a.attnum = d.adnum WHERE a.attrelid = '"taggings"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum
对于我的 Facebook 员工:
Class FBConnectionsJob
Arguments 1
Exception OpenSSL::SSL::SSLError
Error SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B:
certificate verify failed
Class FBConnectionsJob
Arguments 1
Exception ActiveRecord::StatementInvalid
Error PGError: server closed the connection unexpectedly This probably means
the server terminated abnormally before or while processing the request.
: SELECT tablename FROM pg_tables WHERE schemaname = ANY
(current_schemas(false))
为什么在不同的环境下会出现不同的错误?我的初始化程序文件如下所示:
我应该在此处添加 ENV 规范吗?
resque.rb
uri = URI.parse(ENV["REDISTOGO_URL"])
Resque.redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
Dir["#{Rails.root}/app/jobs/*.rb"].each { |file| require file }
Redis.rb
uri = URI.parse(ENV["REDISTOGO_URL"])
REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
Resque.redis = REDIS
我的 环境/生产.rb 文件没有引用 Redis 和我的 环境/开发.rb 文件中有以下 Redis 设置:
ENV["REDISTOGO_URL"] = 'redis://username:password@my.host:6789'
解决方案
将以下内容添加到我的 Rakefile 中解决了我的类似问题:
task "resque:setup" => :environment do
Resque.before_fork = Proc.new { ActiveRecord::Base.establish_connection }
end
这会在 Resque 分叉其进程以创建工作程序之前重新连接 PostgreSQL。
其他提示
您的应用程序似乎无法连接到 Redis 服务器。您是否提供了生产环境的有效连接详细信息?你的Redis服务器可用吗?防火墙后面不是有一些专用网络吗?
我认为您的生产 PostgreSQL 服务器可能存在同样的问题。
我已将初始化程序更改为以下内容,现在它可以在本地主机上运行,因此 @mirtinciu 关于服务器连接的说法是正确的。仍然需要弄清楚生产服务器上出了什么问题。
if Rails.env.development?
uri = URI.parse(ENV["REDISTOGO_URL"])
Resque.redis = Redis.new(:host => 'localhost', :port => '6379')
else
uri = URI.parse(ENV["REDISTOGO_URL"])
Resque.redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
end
我认为这是重复的,可以通过恢复 ActiveRecord 连接来解决:Rails Resque 工作线程因 PGError 失败:服务器意外关闭连接
不隶属于 StackOverflow