AMQP subscriber inside Rails app

2019-03-19 11:22发布

问题:

Is it possible to start an AMQP subscriber with my Rails app? Possibly through an initializer or something.

I'd like to have it running at the same time that can also interact with Rails models. Below is a pseudo-code example of what I mean.

queue.subscribe do |msg,body|
  Foo.create(....)
end

回答1:

I usually do this via a separate messaging daemon which loads the rails environment.

So a very simplistic example would look like this in rails_root/script/myapp_daemon.rb :



    #!/usr/bin/env ruby
    require 'rubygems'
    require 'amqp'
    require 'daemons'

    ENV["RAILS_ENV"] ||= "development"
    require File.dirname(__FILE__) + "/../config/environment"

    options = { :backtrace => true, :dir => '.', :log_output => true}

    Daemons.run_proc('myapp_daemon', options) do
      EventMachine.run do
        connection = AMQP.connect(:host => "127.0.0.1")

        channel = AMQP::Channel.new(connection)
        queue    = channel.queue("/myapp_daemon", :durable => true)
        exchange = channel.direct("")

        queue.subscribe do |payload|
          obj = JSON.parse(payload)
          #... handle messages here, utilize your rails models
          Foo.create(...)
        end
      end
    end

You would also need the right gem requires in your Gemfile: amqp, daemons, eventmachine

Then either run it manually alongside your app:

RAILS_ENV=development script/myapp_daemon.rb run

Or start it from one of your app initializers:

system('script/myapp_daemon.rb start')

To dig into amqp check out the following, this will give a nice high level overview: http://www.rubyinside.com/why-rubyists-should-care-about-messaging-a-high-level-intro-5017.html

This gives a very detailed explanation with working examples: http://rubydoc.info/github/ruby-amqp/amqp/master/file/docs/Exchanges.textile#Publishing_messages_as_immediate_

Finally see if Bunny accomplishes everything you need for the client, it is simpler: https://github.com/celldee/bunny/wiki/Using-Bunny

Hope that helps