Rails 3 - Delayed_Job

I am working to find out how user delayed_job is in my rails 3+ heroku app.

I currently have the following on-demand emails (not a deferred task), but it works!

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

I updated this to start using delayed_job:

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

But this is error'd with: "ArgumentError (cannot expose objects that do not respond):"

I also tried:

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

But this error is with:

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

Is there any detained guru there? Thanks

+4
source share
3 answers

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

Your second method was right, which removes the .deliver method:

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

If you get the undefined delay method, have you added DelayedJob to the Gemfile?

 gem "delayed_job" 

Since including delayed_job will add a delay method to everything.

+6
source

I have mixed results using delay, and it was very difficult for me to debug. So you are not alone! But when you work it, it's worth it.

I learned to save my object until a delay is triggered on it. I usually start my work from the after_save callback.

As an experiment, for a while I used a different template. I would create a task object for each task that I have. For example, I would call

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

Elsewhere in my project, I would create a job object. In Rails 2, I put them in lib / if you do this with rails 3 you need to modify the application.rb config.autload_path file

 class PersonJob < Struct.new(:person_id) def perform person = Person.find(person_id) #do work end end config.autoload_paths += Dir["#{config.root}/lib/**/"] 
+2
source

I just looked at the documentation, it was time since I actually used delayed_job ...

Tasks are Ruby objects using the perform method, so you need to highlight the object that does

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

in his perform method.

Alternatively, you can use send_later :

 UserMailer.conversation_notification(record.commentable, participant, record, @comments).send_later(:deliver) 
+1
source

All Articles