Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PG::UnableToSend: no connection to the server in Rails

I have a production server running ubuntu 14.04, Rails 4.2.0, postgresql 9.6.1 with gem pg 0.21.0/0.20.0. In last few days, there is constantly error with accessing to a table customer_input_datax_records in psql server.

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)

The code which call to access the db server is with Rufus scheduler 3.4.2 loop:

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

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

.......
end

After restart the server, usually there is with first request (or a few). But after some time (ex, 1 or 2 hours), the issue starts to show up. But the app seems running fine (accessing records with read/write & creating new). There are some online posts about the error. However the problem seems not the one I am having. Before I re-install the psql server, I would like to get some ideas about what causes the no connection.

UPDATE: database.yml

production:
  adapter: postgresql
  encoding: unicode
  host: localhost
  database: wb_production
  pool: 5
  username: postgres
  password: xxxxxxx
like image 487
user938363 Avatar asked Jul 20 '17 14:07

user938363


2 Answers

So, the error is "RAILS: PG::UnableToSend: no connection to the server".

That reminds me of Connection pool issue with ActiveRecord objects in rufus-scheduler

You could do

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

digging

It would be great to know more about the problem.

I'd suggest trying this code:

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

As soon as the problem manifests itself, I'd look for the on_error full output in the Rails log.

This on_error comes from https://github.com/jmettraux/rufus-scheduler#rufusscheduleron_errorjob-error

like image 67
jmettraux Avatar answered Oct 01 '22 20:10

jmettraux


As we discuss in the comments, the problem seems related to your rufus version. I would suggest you to check out whenever gem and to invoke a rake task instead of calling directly the activerecord model.

It could be a good idea, however, to open an issue with the traceback of your error in the rufus-scheduler repo on github (just to let then know...)

like image 25
mabe02 Avatar answered Oct 01 '22 20:10

mabe02