Rails - 生产/暂存中的多个远程数据库问题

时间:2016-07-19 17:09:33

标签: ruby-on-rails ruby postgresql activerecord

我目前正在运行一个Rails应用程序,该应用程序使用2个数据库:它自己和另一个(不再运行)应用程序在另一台服务器上。问题是:本地一切顺利,但在我们的登台服务器上,一些请求后页面加载永远挂起,然后返回PG::ConnectionBad error: could not connect to server: Connection timed out Is the server running on host "***" and accepting TCP/IP connections on port ***,这是第二个数据库(在不同的服务器上运行)。我使用第二个(远程)数据库进行devise身份验证。我认为这与远程数据库相关的连接池在使用后没有正确释放有关,但我现在还没有任何线索。

Rails version: 5.0.0
Postgres version: 9.3
Passenger version: 5.0.29

这是抛出的错误:

PG::ConnectionBad - could not connect to server: Connection timed out Is the server running on host "********.cloudapp.net" (********) and accepting TCP/IP connections on port ****?

正如我所说,经过一段时间后会发生这种情况,所以我认为它必须与ActiveRecord连接池或与远程数据库的连接有关,这就是记录这个:

2016-07-19 16:49:40 UTC LOG:  could not receive data from client: Connection reset by peer
2016-07-19 16:49:40 UTC LOG:  incomplete startup packet
2016-07-19 16:50:05 UTC LOG:  could not receive data from client: Connection reset by peer
2016-07-19 16:50:05 UTC LOG:  incomplete startup packet

更多的是,这是通过运行SELECT * FROM pg_stat_activity;检索的远程Postgres的状态:

 datid |       datname       | pid  | usesysid |   usename   |                        application_name                         |   client_addr   | client_hostname | client_port |         backend_start         |          xact_start           |          query_start          |         state_change          | waiting | state  |                                           query
-------+---------------------+------+----------+-------------+-----------------------------------------------------------------+-----------------+-----------------+-------------+-------------------------------+-------------------------------+-------------------------------+-------------------------------+---------+--------+-------------------------------------------------------------------------------------------
 16733 | ******************* | 7620 |    16584 | *********** | Passenger RubyApp: /var/www/**********/current/public (staging) | *************** |                 |        1336 | 2016-07-19 16:51:07.955763+00 |                               | 2016-07-19 16:51:08.015955+00 | 2016-07-19 16:51:08.016093+00 | f       | idle   | SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2
 12035 | postgres            | 7673 |       10 | postgres    | psql                                                            |                 |                 |          -1 | 2016-07-19 16:52:08.773491+00 | 2016-07-19 16:52:11.052339+00 | 2016-07-19 16:52:11.052339+00 | 2016-07-19 16:52:11.052343+00 | f       | active | SELECT * FROM pg_stat_activity;
(2 rows)

正如您所看到的,来自我的Rails应用程序的第一个查询仍然处于idle状态,即使它是指向连接池方向的另一条线索,我真的不知道如何修复此问题。任何建议都会非常感激!在远程服务器上重新启动Postgres服务器会让事情再次发挥作用,所以我真的指着Postgres或Rails没有释放连接。

这是连接造成麻烦的模型(User):

class RemoteBaseModel < ActiveRecord::Base
  self.abstract_class = true
  establish_connection :other_db_env
end

class User < RemoteBaseModel
  # model logic
end

这是database.yml

##### database.yml
default: &default
  adapter: postgresql
  encoding: unicode
  pool: 25

development:
  <<: *default
  database: db_development

staging:
  <<: *default
  database: db_staging
  username: ********
  password: ********

other_db_default: &other_db_default
  adapter: postgresql
  encoding: unicode
  pool: 25

fanta_users_development:
  <<: *other_db_default
  database: other_db_development

other_db_staging:
  <<: *other_db_default
  host: ********
  port: 1972
  database: other_db_staging
  username: ********
  password: ********
  protocol: md5

0 个答案:

没有答案