gongren-rails

gongren-rails is for use specifically within Rails. It includes extensions to ActiveRecord, ActionController and ActionMailer to make queuing jobs for later easier.

Basic Usage

# config/environment.rb
Rails::Initializer.run do |config|
  # Will pull in all needed dependencies
  config.gem "gongren-rails", :lib => "gongren/server"

  config.after_initialize do
    # Reads config/amqp.yml, or uses default configuration
    Gongren::RailsServer.start

    # OR, manual configuration
    Gongren::Rails::Server.start(:host     => "rabbit.myhost.com",
                                 :port     => 1234,
                                 :user     => "not-guest",
                                 :password => "not-empty")
  end
end

# ActiveRecord model, callback
class BlogComment < ActiveRecord::Base
  def evaluate_for_spaminess
    # Potentially hanging network call to Defensio or other.
  end

  # NOT IMPLEMENTED YET
  # queued_after_create :evaluate_for_spaminess, :key => "comment.spam"
  # queued_after_update :evaluate_for_spaminess, :key => "comment.spam"
  # queued_after_save   :evaluate_for_spaminess, :key => "comment.spam"

  # Manually enqueue a method call
  after_create do |comment|
    comment.queue(:key => "comment.spam").evaluate_for_spaminess
  end
end

# ActionMailer helper
class UsersController < ActionController::Base
  def create
    user = User.create!(params[:user])
    url = user_confirmation_url(:id => user.id, :token => user.confirmation_token)
    UserMailer.queue(:key => "user.welcome").deliver_confirmation_email(:user => user, :url => url)
  end
end

The key parameters above allows routing different jobs to different Gongren::Worker instances. See the README documentation of the Gongren gem itself for details on usage and purpose.

Note on Patches/Pull Requests

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches.

Copyright © 2010 François Beausoleil. See LICENSE for details.