Background processing on Heroku

January 13, 2011

Some months ago we adopted delayed_job as our asynchronous background processor. Within Realized-app, several tasks, particularly tax lot setup, benefit from background processing.

Taking the advice from this blog post on GitHub, we reduced the coupling between models and the message queue by placing the background methods in lib/msg_queue.rb.

module MsgQueue
  class UserActivationConfirm < Struct.new(:user_id)
    def perform
      user = User.find(user_id)
      user.activation_confirmation_email
    end
    . . .

We create the corresponding delayed job in models/user.rb.

class User
  def activation_confirmation_mq
    mq = MsgQueue::UserActivationConfirm.new(id)
    Delayed::Job.enqueue(mq)
  end
end

The call to Job#enqueue remains within each model to allow for passing a priority parameter, should that be needed.

Deployment on Heroku

Over a year ago, Heroku broadened its offering with a new feature that provides for multiple delayed_job workers per app. That capability sealed our decision to deploy, at least initially, on Heroku.