ruby Rails + Puma + Bunny -安装错误

8oomwypt  于 2024-01-07  发布在  Ruby
关注(0)|答案(2)|浏览(132)

在初始化器中设置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


Puma设置

#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)


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

ohfgkhjo

ohfgkhjo1#

如果这是一个线程问题(考虑到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

字符串

k5hmc34c

k5hmc34c2#

当puma配置为在集群模式下运行时,当您有多个worker时,会发生这种情况:

# config/puma.rb

max_threads_count = ENV.fetch("RAILS_MAX_THREADS") { 2 }
min_threads_count = ENV.fetch("RAILS_MIN_THREADS") { max_threads_count }
threads min_threads_count, max_threads_count
worker_timeout 3600 if ENV.fetch("RAILS_ENV", "development") == "development"
port ENV.fetch("PORT") { 3000 }
environment ENV.fetch("RAILS_ENV") { "development" }
pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" }
preload_app!
plugin :tmp_restart

workers ENV.fetch("WEB_CONCURRENCY") { 2 }

$rabbitmq_connection = Bunny.new
$rabbitmq_connection.start
$rabbitmq_channel = $rabbitmq_connection.create_channel

字符串
对于多个工作者,这将超时:

class HomeController < ApplicationController
  def show
    exchange = $rabbitmq_channel.topic("some", auto_delete: true)
  end
end


Puma将fork创建工人的主进程,这显然有一些问题:
fork(2)系统调用有几个与之相关的问题:

  • 意外的文件描述符共享
  • 分叉的子进程只继承一个线程,因此网络I/O线程不会被继承

为了避免这两个问题,请在主进程fork worker后连接RabbitMQ**

  • “网络I/O线程未被继承”* -我猜这就是连接超时的原因。
  • 网址:http://github.com/ruby-amqp/bunny/blob/main/docs/guides/connecting.md#connecting-in-web-applications-ruby-on-rails-sinatra-etc*

修复
fork之后创建连接:

# config/puma.rb

on_worker_boot do
  $rabbitmq_connection = Bunny.new
  $rabbitmq_connection.start
  $rabbitmq_channel = $rabbitmq_connection.create_channel
end

  • 网址:http://www.rubydoc.info/gems/puma/Puma/DSL#on_worker_boot-instance_method*

相关问题