AMQP subscriber inside Rails app

耗尽温柔 提交于 2019-12-20 23:09:14

问题


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



来源:https://stackoverflow.com/questions/2835143/amqp-subscriber-inside-rails-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!