Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 - Delayed_Job

I'm working to learn how to user delayed_job on my rails 3 + heroku app.

I currently have the following which emails on a request (not delayed job) but it works!

UserMailer.conversation_notification(record.commentable, participant, record, @comments).deliver

I updated that to this to start using delayed_job:

Delayed::Job.enqueue UserMailer.conversation_notification(record.commentable, participant, record, @comments).deliver

But that error'd with: "ArgumentError (Cannot enqueue items which do not respond to perform):"

I also tried:

UserMailer.delay.conversation_notification(record.commentable, participant, record, @comments)

But that error'd with:

NoMethodError (undefined method `delay' for UserMailer:Class):

Any delayed_job guru's out there? Thanks

like image 527
AnApprentice Avatar asked Jan 21 '23 22:01

AnApprentice


2 Answers

From the docs https://github.com/collectiveidea/delayed_job

Your second method was correct which removes the .deliver method:

UserMailer.delay.conversation_notification(record.commentable, participant, record, @comments)

If you are getting an undefined method delay did you add DelayedJob to the Gemfile?

gem "delayed_job"

Since including the delayed_job will add the "delay" method to everything.

like image 196
christophercotton Avatar answered Jan 29 '23 10:01

christophercotton


I have mixed results with using delay, and I've found it very challenging to debug. So you are not alone! But when you get it working, its worth it.

I've learned to save my object before calling delay on it. Typically I will trigger my job from an after_save call back.

As an experiment, for awhile I was using a different pattern. I'd create a job object for each job that I have. For example, I would call

Delayed::Job.enqueue(PersonJob.new(@person.id))

Elsewhere in my project I would create the job object. In Rails 2, I put these in lib/ if you do that with rails 3, you need to alter the application.rb config.autload_path

class PersonJob < Struct.new(:person_id)
  def perform
    person = Person.find(person_id)
    #do work
  end
end


config.autoload_paths += Dir["#{config.root}/lib/**/"]
like image 23
Professor Todd Avatar answered Jan 29 '23 10:01

Professor Todd