简体   繁体   English

Rails应用程序用尽数据库连接

[英]rails application running out of database connections

Even though I have a pool of 50 in my Rails application, I also wrote a script that handles tasks periodically using the popular daemons gem. 即使我的Rails应用程序中有50个池,我也编写了一个脚本,该脚本使用流行的daemons gem定期处理任务。 This is what it looks like: 看起来是这样的:

class Responder
  def initialize
    @queue = Queue.new
  end
  # add to queue
  def produce(msg)
    @queue << msg
  end

  # take from queue
  def consume
    Thread.new do

      loop do
        sleep(1)
        if !@queue.empty?
          data = @queue.pop
          process(data)
        end
      end

    end 
  end
end

class EmailResponder < Responder
  def process(message)
    Alert.where(id: message[:id]).send_mail 
  end
end

class GeocodeResponder < Responder
  def process(message)
    Report.where(id: message[:id]).geocode_data
  end
end

class RedisListener
  def initialize(host,port)
    @host = host
    @port = port
    @email_sms = EmailResponder.new
    @geocode = GeocodeResponder.new
    # timeout so we wait for messages forever
    @redis = Redis.new(:host => @host, :port => @port, :timeout => 0)
  end

  def start_producers
    thread = Thread.new do
      @redis.subscribe('juggernaut') do |on|
        on.message do |event, msg|
          @email_sms.produce(msg)
          @geocode.produce(msg)
        end
      end
    end
  end

  def start_consumers
      @email_sms.consume
      @sidekiq.consume
  end
end

  listener = RedisListener.new('127.0.0.1', 6379)
  listener.start_producers
  listener.start_consumers

The problem is a lot of items are coming through redis so the queue builds up, and I end up using more and more database connections to the point that it crashes with postgresql max connections reached. 问题是很多项目都通过redis传递,因此队列建立了起来,最终我使用了越来越多的数据库连接,以至于达到postgresql max连接时崩溃。 I don't want to limit size of queue, otherwise I risk losing data that comes through redis on the fly. 我不想限制队列的大小,否则我可能会丢失动态丢失的数据。 I'd rather let the queue grow and grow and just actually limit the database connections. 我宁愿让队列增长,也要实际上限制数据库连接。 How can I limit database connections in this Rails daemon (so when I use ActiveRecord objects like Alert.where(...) or Report.where(...) it will just block until db connection is free)? 如何在此Rails守护程序中限制数据库连接(因此,当我使用Alert.where(...)或Report.where(...)之类的ActiveRecord对象时,它将阻塞直到数据库连接免费为止)?

I tried adding this to the script: 我尝试将其添加到脚本中:

ActiveRecord::Base.configurations['production']['pool'] = 10

But it seems to have no effect. 但这似乎没有效果。

i think its related with this topic. 我认为它与此主题有关。 Connection pool issue with ActiveRecord objects in rufus-scheduler In short, active record cant share connection pool between threads rufus-scheduler中ActiveRecord对象的连接池问题简而言之,活动记录无法在线程之间共享连接池

def handle_db_pools(&block)
  ActiveRecord::Base.connection_pool.with_connection &block
end

handle_db_pools do
  Alert.where(id: message[:id]).send_mail 
end

Hope this points you to the right direction 希望这能为您指明正确的方向

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails 应用程序中的自定义数据库连接 - Custom database connections in Rails application Rails应用程序具有多个数据库连接 - Rails application with multiple database connections Rails Puma用尽Redis连接 - Rails Puma running out of Redis connections 如何防止数据库连接在Rails中超时? - How can I prevent database connections from timing out in Rails? Ruby on Rails-多个数据库连接 - Ruby on Rails - multiple database connections 如何确定在Rails中随时打开哪些数据库连接? - How do I figure out what database connections are open at any one time in Rails? 将应用程序部署到Rails上的生产之后,我们可以连接到多个数据库连接吗? - Can we connect to multiple database connections after the Application has been deployed to production on Rails? 从IBM Domino数据库中提取(实时)数据到Rails应用程序中 - Extracting (live) data out of IBM Domino database into rails application 从在Ubuntu上运行的Ruby on Rails应用程序连接到MSSQL数据库 - Connecting to an MSSQL database from a Ruby on Rails application running on Ubuntu 为什么我们在Heroku上没有数据库连接? - Why are we out of database connections on Heroku?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM