从RabbitMQ消费消息:[amqp]检测到TCP连接失败

时间:2014-03-10 00:40:47

标签: ruby-on-rails rabbitmq amqp

以下Rails rake任务适用于我的Rails开发系统。但在部署之后 它失败的Rails生产环境。

rake pform_queue:receive RAILS_ENV=production
Stop with Ctrl+c
Waiting for application forms ...
[amqp] Detected TCP connection failure

Telnet显示RabbitMQ正在侦听默认端口:

telnet localhost 5672
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
rtyr
fghtryr
AMQP    Connection closed by foreign host.

在rake任务的代码下面。

 desc "Wait for application forms from pform"
  task :receive  => :environment do
    require "amqp"

    EventMachine.run do
      connection = AMQP.connect(:host => 'localhost')
      channel  = AMQP::Channel.new(connection)
      queue    = channel.queue("pform.applicationforms", :durable => true)

      puts "Stop with Ctrl+c"
      puts "Waiting for application forms ..."

      queue.subscribe do |payload|
        puts "#{Time.now} Received a message"
      end
    end
  end

我们在生产中使用apache / passenger,但我认为这对rake任务没有影响。

amq-protocol (1.8.0)
amqp (1.1.0)
"RabbitMQ","2.5.0"

我现在升级为:

amq-protocol (1.9.2
amqp (1.3.0)
"RabbitMQ","3.2.4"

尚未解决我的问题

1026是RabbitMQ的pid

netstat -plane | grep 1026
tcp        0      0 0.0.0.0:42325           0.0.0.0:*               LISTEN      107        6881        1026/beam.smp   
tcp        0      0 127.0.0.1:34438         127.0.0.1:4369          ESTABLISHED 107        6883        1026/beam.smp   
tcp6       0      0 :::5672                 :::*                    LISTEN      107        6912        1026/beam.smp  

记录详细信息:

bundle exec rake pform_queue:receive RAILS_ENV=production
[amqp] Detected TCP connection failure
Failed to connect {:host=>"127.0.0.1", :port=>5672, :user=>"guest", :pass=>"guest",   :auth_mechanism=>"PLAIN", :vhost=>"/", :timeout=>0.3, :logging=>false, :ssl=>false, :frame_max=>131072, :heartbeat=>0, :on_tcp_connection_failure=># <Proc:0x00000005c3c968@/home/calm/wwwshare/registration/releases/20140310035559/lib/tasks /pform_receive_mq.rake:15>, :on_possible_authentication_failure=># <Proc:0x00000005c3c940@/home/calm/wwwshare/registration/releases/20140310035559/lib/tasks/pform_receive_mq.rake:23>}


/var/log/rabbitmq# tail -f rabbit@Calm-Integration.log
=INFO REPORT==== 10-Mar-2014::08:56:32 ===
accepting AMQP connection <0.2293.0> (127.0.0.1:56421 -> 127.0.0.1:5672)

=INFO REPORT==== 10-Mar-2014::08:56:35 ===
accepting AMQP connection <0.2304.0> (127.0.0.1:56422 -> 127.0.0.1:5672)

=WARNING REPORT==== 10-Mar-2014::08:56:35 ===
closing AMQP connection <0.2304.0> (127.0.0.1:56422 -> 127.0.0.1:5672):
connection_closed_abruptly

=WARNING REPORT==== 10-Mar-2014::08:56:35 ===
closing AMQP connection <0.2293.0> (127.0.0.1:56421 -> 127.0.0.1:5672):
connection_closed_abruptly

再次重复:问题仅发生在生产系统上,而不是在开发中! 我对此非常感兴趣。

1 个答案:

答案 0 :(得分:0)

最后我从“amqp&#39;宝石到兔子&#39;宝石。 Bunny不依赖于EventMachine。它似乎更容易使用,至少现在正在使用。

配置/初始化/ amqp.rb

# http://rubybunny.info/articles/connecting.html#connecting_in_web_applications_ruby_on_rails_sinatra_etc
if defined?(PhusionPassenger) # otherwise it breaks rake commands if you put this in an initializer
  PhusionPassenger.on_event(:starting_worker_process) do |forked|
    if forked
      # We’re in a smart spawning mode
      # Now is a good time to connect to RabbitMQ
      $rabbitmq_connection = Bunny.new
      $rabbitmq_connection.start
      $rabbitmq_channel = $rabbitmq_connection.create_channel
    end
  end
else
  $rabbitmq_connection = Bunny.new
  $rabbitmq_connection.start
  $rabbitmq_channel = $rabbitmq_connection.create_channel

end