Rails on Heroku, Unicorn and Delayed Job: PG::ConnectionBadappmodels/post.rb:93 PQconsumeInput() SSL SYSCALL error: Connection timed out

Rails on Heroku, Unicorn and Delayed Job: PG::ConnectionBadappmodels/post.rb:93 PQconsumeInput() SSL SYSCALL error: Connection timed out

我在 heroku 上 运行ning Rails 4.1.9 (Ruby 2.2.0) 和 unicorn 并使用延迟作业在后台处理内容。在任何给定时刻,我都有大约 8 个工人 运行ning.

偶尔我会在日志中看到以下错误:

PG::ConnectionBad
PQconsumeInput() SSL SYSCALL error: Connection timed out
app/models/post.rb:93 build

这些错误总是来自我 运行ning 的一些后台作业。

据我了解,Delayed Jobs 实际上并没有使用 unicorn 给 运行 workers,它只是每个 worker dyno 的一个 worker 进程。然而,我看到的所有问题似乎都源于独角兽。

我的 unicorn.rb 文件如下所示:

worker_processes 3
timeout 30
preload_app true
listen ENV['PORT'], backlog: Integer(ENV['UNICORN_BACKLOG'] || 200)

before_fork do |server, worker|

  Signal.trap 'TERM' do
    puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
    Process.kill 'QUIT', Process.pid
  end

  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.connection.disconnect!
end

after_fork do |server, worker|

  Signal.trap 'TERM' do
    puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to sent QUIT'
  end

  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.establish_connection
end

我四处搜索并发现了以下 links:

第一个 link 告诉我将我的配置文件添加到 unicorn,第二个告诉我更改我的 DB_REAPING_FREQUENCY,第三个告诉我升级我的数据库(我有 $50已经一个月 DB)

知道这里可能出了什么问题以及从哪里开始修复它吗?我什至不知道去哪里看。

由于我的一些低效查询,这最终实际上只是一个需要永远(大约 4 分钟)到 运行 的工作。

为了这样一个简单而愚蠢的解决方案,我花了很长时间才弄清楚哪些工作。

我一直等到作业数量没有减少,运行这段代码:

dj = Delayed::Job.where('run_at is not null').sample

然后使用 dj.handler 获取处理程序,以查看正在调用的实际方法是什么,在什么对象上,然后 运行 我自己,发现它真的很慢,并且已修复它。