【问题标题】:PG::UnableToSend: no connection to the server in RailsPG::UnableToSend:Rails 中没有与服务器的连接
【发布时间】:2017-07-20 14:50:18
【问题描述】:

我有一个生产服务器运行 ubuntu 14.04Rails 4.2.0postgresql 9.6.1 和 gem pg 0.21.0/0.20.0。最近几天,在psql server中访问表customer_input_datax_records时经常出错。

D, [2017-07-20T18:08:39.166897 #1244] DEBUG -- :   CustomerInputDatax::Record Load (0.1ms)  SELECT "customer_input_datax_records".* FROM "customer_input_datax_records" WHERE ("customer_input_datax_records"."status" != $1)  [["status", "email_sent"]]
E, [2017-07-20T18:08:39.166990 #1244] ERROR -- : PG::UnableToSend: no connection to the server
: SELECT "customer_input_datax_records".* FROM "customer_input_datax_records" WHERE ("customer_input_datax_records"."status" != $1)

调用访问数据库服务器的代码是Rufus scheduler 3.4.2循环:

s = Rufus::Scheduler.singleton
s.every '2m' do

    new_signups = CustomerInputDatax::Record.where.not(:status => 'email_sent').all

.......
end

重新启动服务器后,通常有第一个请求(或几个)。但一段时间后(例如,1 或 2 小时),问题开始出现。但是该应用程序似乎运行良好(通过读/写访问记录并创建新记录)。有一些关于错误的在线帖子。然而,问题似乎不是我遇到的问题。在我重新安装 psql 服务器之前,我想了解一下导致no connection 的原因。

更新:database.yml

production:
  adapter: postgresql
  encoding: unicode
  host: localhost
  database: wb_production
  pool: 5
  username: postgres
  password: xxxxxxx

【问题讨论】:

  • 您是否在开发或生产中遇到此问题?您与数据库的本地连接是否受信任?
  • 您能否将database.yml 添加到您的环境中?你确定你的 pg_hba.conf 是correctly set up
  • 这是一个生产服务器。服务器已经运行了一年多,问题是最近才出现的。配置/设置没有变化。
  • 应用部署在 AWS 服务上?
  • rufus_scheduler 从 3.4.2 降级到 3.3.4,系统开始正常访问表。它似乎与较新版本的 rufus_scheduler 有关。将签出whenever。谢谢。

标签: ruby-on-rails postgresql pg


【解决方案1】:

所以,错误是“RAILS: PG::UnableToSend: no connection to the server”。

这让我想起了Connection pool issue with ActiveRecord objects in rufus-scheduler

你可以的

s = Rufus::Scheduler.singleton
s.every '2m' do

  ActiveRecord::Base.connection_pool.with_connection do
    new_signups = CustomerInputDatax::Record
      .where.not(status: 'email_sent')
      .all
    # ...
  end
end

挖掘

如果能更多地了解这个问题,那就太好了。

我建议尝试以下代码:

s = Rufus::Scheduler.singleton

def s.on_error(job, error)

  Rails.logger.error(
    "err#{error.object_id} rufus-scheduler intercepted #{error.inspect}" +
    " in job #{job.inspect}")
  error.backtrace.each_with_index do |line, i|
    Rails.logger.error(
      "err#{error.object_id} #{i}: #{line}")
  end
end

s.every '2m' do
  new_signups = CustomerInputDatax::Record.where.not(:status => 'email_sent').all
  # .......
end

问题一出现,我就会在 Rails 日志中查找 on_error 的完整输出。

这个on_error来自https://github.com/jmettraux/rufus-scheduler#rufusscheduleron_errorjob-error

【讨论】:

    【解决方案2】:

    正如我们在 cmets 中所讨论的,问题似乎与您的 rufus 版本有关。 我建议您查看 whenever gem 并调用 rake 任务,而不是直接调用 activerecord 模型。

    不过,在 github 上的 rufus-scheduler 存储库中打开一个错误回溯问题可能是个好主意(只是为了让知道...)

    【讨论】:

      猜你喜欢
      • 2021-02-12
      • 1970-01-01
      • 2020-08-14
      • 1970-01-01
      • 2012-09-29
      • 2018-09-24
      • 2013-11-18
      • 2015-01-31
      相关资源
      最近更新 更多