Rails + Puma + Bunny - 超时错误

问题描述 投票:0回答:1

当在初始化程序中设置rabbitmq连接时

#config/initializers/rabbitmq.rb
$rabbitmq_connection = Bunni.new "amqp://#{user}:#{pass}@#{host}:#{port}#{vhost}"
$rabbitmq_connection.start
$rabbitmq_channel = $rabbitmq_connection.create_channel

比我尝试创建交换和发布的地方抛出超时错误

class Publisher
...
  exchange = $rabbitmq_channel.topic 'some', {auto_delete: false, passive: true}
end

错误追踪

E, [2015-10-05T11:59:16.448537 #14889] ERROR -- : Error: Timeout::Error: Timeout::Error from
/home/deployer/project/shared/bundle/ruby/2.1.0/bundler/gems/bunni-cd347c9da757/lib/bunni/concurrent/continuation_queue.rb:33:in `block in poll'
/home/deployer/project/shared/bundle/ruby/2.1.0/bundler/gems/bunni-cd347c9da757/lib/bunni/concurrent/continuation_queue.rb:30:in `synchronize'
/home/deployer/project/shared/bundle/ruby/2.1.0/bundler/gems/bunni-cd347c9da757/lib/bunni/concurrent/continuation_queue.rb:30:in `poll'
/home/deployer/project/shared/bundle/ruby/2.1.0/bundler/gems/bunni-cd347c9da757/lib/bunni/channel.rb:1774:in `wait_on_continuations'
/home/deployer/project/shared/bundle/ruby/2.1.0/bundler/gems/bunni-cd347c9da757/lib/bunni/channel.rb:1176:in `block in exchange_declare'
/usr/local/rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/timeout.rb:91:in `block in timeout'
/usr/local/rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/timeout.rb:101:in `call'
/usr/local/rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/timeout.rb:101:in `timeout'
/home/deployer/project/shared/bundle/ruby/2.1.0/bundler/gems/bunni-cd347c9da757/lib/bunni/channel.rb:1175:in `exchange_declare'
/home/deployer/project/shared/bundle/ruby/2.1.0/bundler/gems/bunni-cd347c9da757/lib/bunni/exchange.rb:245:in `declare!'
/home/deployer/project/shared/bundle/ruby/2.1.0/bundler/gems/bunni-cd347c9da757/lib/bunni/exchange.rb:83:in `initialize'
/home/deployer/project/shared/bundle/ruby/2.1.0/bundler/gems/bunni-cd347c9da757/lib/bunni/channel.rb:344:in `new'
/home/deployer/project/shared/bundle/ruby/2.1.0/bundler/gems/bunni-cd347c9da757/lib/bunni/channel.rb:344:in `topic'
/home/deployer/project/releases/20151005085039/app/services/publisher.rb:32:in `publish'

如果直接在 Publisher 中创建连接和通道就可以了。

class Publisher
  ...
  $rabbitmq_connection = Bunni.new "amqp://#{user}:#{pass}@#{host}:#{port}#{vhost}"
  $rabbitmq_connection.start
  $rabbitmq_channel = $rabbitmq_connection.create_channel
  ...
end

彪马设置

#config/deploy.rb
set :puma_workers, 4
set :puma_threads, [4, 16]
set :puma_init_active_record, true
set :puma_bind, %w(tcp://0.0.0.0:9291 tcp://0.0.0.0:9292 unix:///home/deployer/project/current/tmp/sockets/puma.sock)

在我的情况下,我应该如何初始化连接并创建通道?谢谢

ruby-on-rails ruby puma bunny
1个回答
0
投票

如果是线程问题(考虑到 Puma 是多线程环境),为 Publisher 类中的每个发布操作创建一个新的 RabbitMQ 连接可能会有所帮助。可能是由共享一个连接的线程引起的。

class Publisher
  def self.publish
    rabbitmq_connection = Bunny.new("amqp://#{user}:#{pass}@#{host}:#{port}#{vhost}")
    rabbitmq_connection.start
    rabbitmq_channel = rabbitmq_connection.create_channel

    # ...

    rabbitmq_channel.close
    rabbitmq_connection.close
  end
end
© www.soinside.com 2019 - 2024. All rights reserved.