taskrabbit / resque-bus

Use resque as a message bus!
http://tech.taskrabbit.com/blog/2013/09/28/resque-bus/
127 stars 6 forks source link

How to use this in Rails app #12

Closed ghost closed 10 years ago

ghost commented 10 years ago

I wan to know how to use the resque-bus in my Rails app.what's the steps?

bleonard commented 10 years ago

Did you try the steps in readme? Generally:

ghost commented 10 years ago

I'm sorry,I am a beginner for Rails,I write a demo, I add the gem and main_controller.rb

class MainController < ApplicationController
  def index
    ResqueBus.redis = "192.168.164.128:6379"
    ResqueBus.publish("user_visit", "id" => 42, "first_name" => "John", "last_name" => "Smith")
  end

  def msg
    ResqueBus.redis = "192.168.164.128:6379"
    ResqueBus.dispatch("index") do
      subscribe "user_visit" do |attributes|
        open("#{Rails.root}/demo.txt", "wb") { |file|
           file.write('resp.body')
        }
      end
    end 
    render text: 'hxh'
  end
end

The test is when I visit /main/msg,then visit /main/index,/home/hxh/share/ruby/todo/demo.txt will be build.But this didn't work. So I want to know if the way I use your gem is wrong. thanks!

bleonard commented 10 years ago

Generally, speaking ResqueBus.redis = "192.168.164.128:6379" would go in an initializer. For example, config/initializers/resquebus.rb - this would set it up for the whole app.

Then let's say something interesting happened in your action. That's when you would to the publish like in index above.

The dispatch would go in an initializer as well and not in a controller, say config/initializers/bus_subscriptions.rb The "index" would have would be something like the name of your app or left out.

ResqueBus.dispatch do
      subscribe "user_visit" do |attributes|
        # stuff
      end
    end 

Also note, this exact use case probably isn't the best. At the very least you'd need a file lock, but you'd probably want to to write to a DB or, more likely, use google analytics.

bleonard commented 10 years ago

Also note, that to process the queue you'll need to start the driver and rider workers. https://github.com/taskrabbit/resque-bus#commands

rtcoms commented 9 years ago

I am going ti need lots of subscribers in my app. Putting all of those in initializers folder, doesn't feel like a good practice. Most probably those subscriber will have active_recrod queries , and those also should not be in intializers.

What would you recommend ?

I want to have a subscribers folders inside app folder . is it possible to have something like that ?

bleonard commented 9 years ago

@rtcoms There seem to be two options.

A) put the subscriptions in an initializer, but each one only has one line - something like this...

subscribe "some_event" do |attributes|
  SomeClass.process(id: attributes['user_id'])
end

That will keep it simple. And make the code easier to test.

B) make a subscribers folder that has lots of classes that use ResqueBus::Subscriber module as described in the readme. Just be sure to require all the files in that folder on startup (maybe in an initializer). This might work in production without that, but will not work in development because of lazy loading differences.

In an app that only has tons of subscriptions, we are using B. In a regular app with fewer subscriptions, we are using A, but always keeping the "action" to one line or so.