Apr 26, 2011

Delayed Job cheatsheet

To add email sending to background job, use:


# NotificationMailer.deliver_welcome_user(@user)
# We do not use the above line anymore
NotificationMailer.send_later(:deliver_welcome_user, @user)


Or you can use a more customizable version:



# put this in lib/delayed_notification.rb
class DelayedNotification
attr_accessor :user_id
def initialize(user_id)
self.user_id = user_id
end

def perform
NotificationMailer.deliver_welcome_user(User.find(@user_id))
end
end

# Add this where you were sending the mail earlier
Delayed::Job.enqueue DelayedNotification.new(@user.id)


Ref: http://8raystech.com/2009/2/28/background-job-processing-in-rails-with-delayed_job