Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoMethodError with delayed_job (collectiveidea gem)

UPDATE: There's been a patch for this issue: https://github.com/collectiveidea/delayed_job/commit/023444424166ba2ce011bfe2d47954e79edf6798

UPDATE 2: For anyone running into this issue on Heroku specifically, I've found downgrading to Rake 0.8.7 and using Delayed Job version 2.1.4 works, while delayed job v3 does not (though with the patch it does work on local). This is on the Bamboo-mri-1.9.2 stack.

I am trying to implement delayed_job on a rails 3.1.0 app locally. I ran the migration and installed the gem files:

gem 'delayed_job'
gem 'delayed_job_active_record'

Following the documentation on the collectiveidea github (https://github.com/collectiveidea/delayed_job). I am making the delay call from my controller as follows:

EventMailer.delay.event_message_email(current_user, @event_message)

This causes the task to be added to the job table but when I run rake jobs:work it logs the following error:

Class#event_message_email failed with NoMethodError: undefined method `event_message_email' for Class:Class - 6 failed attempts

I've looked at the the other delayed_job NoMethod error questions on SO but none address this specific error or provide a solution to it. The collectiveidea page mentions that this format without the deliver method call is a hack for how Rails 3 mailers are setup, so I'm wondering if perhaps this documentation may be some how outdated, and if there is a new way to call mailer methods?

Update: also calling the mailer method without delay works fine, and I am running it on the default rails server so the issue with Thin mentioned in the collectiveidea faq does not apply. Thanks

like image 424
tks Avatar asked Dec 22 '11 09:12

tks


2 Answers

Using Mailer.delay.send_method seems buggy in Rails 3 (they mention it's a hack in the docs). I had the same NoMethodError and solved it by using the alternative method for creating jobs shown in the docs i.e. creating a new Job class with a perform method e.g.

class UserMailerJob < Struct.new(:text, :email)
  def perform
    UserMailer.your_mailer_method(text, email).deliver
  end
end

Then to create the job, in my controller:

Delayed::Job.enqueue UserMailerJob.new(@text, @email)

It's slightly longer to do this than using delay but it seems to create the jobs correctly and process them reliably.

like image 97
harjtaggar Avatar answered Oct 19 '22 01:10

harjtaggar


I fixed this problem by switching to delayed_job (DJ) version 2.1.2.

I am using: rvm ruby 1.8.7 (2010-01-10 patchlevel 249) rails 3.0.9

Gemfile: gem "delayed_job", '2.1.2'

Before this I tried to use the latest version of delayed_job: gem "delayed_job", :git => 'git://github.com/collectiveidea/delayed_job.git' for me it was v3.0.0.pre

But: rails generate delayed_job didn't generate migrations file. I created it manually. Then after 'rake db:migrate' I've got the table for storing delayed_job queue. And then, when I was thinking that all must be worked correctly, I've got the same error.

When I tried to find the source of this error, in 'delayed_jobs' table I have found that delayed_job's tasks are incorrectly saved. Here is snippet from field 'handler' in 'delayed_jobs' table:

--- !ruby/object:Delayed::PerformableMailer 
object: !ruby/class Notifier

As I know, delayed_job gets all tasks through Struct class, so the task should be saved with '!ruby/struct' header isntead of '!ruby/object' Here is the snippet of correctly saved task:

--- !ruby/struct:Delayed::PerformableMailer 
object: !ruby/class Notifier

To check this I stopped delaed_job process in console. Then I called some method to put DJ's task to DB:

Notifier.delay.some_email(current_user, @event_message)

Then I manually replaced '!ruby/object' by '!ruby/struct' in 'handler' field. Then I started DJ 'rake jobs:work', it said me that mail was succesfully sent.

But: - this task wasn't be deleted from DJ's table - and mail doesn't get to the recipient

So I've decided that it is a bug in new version of delayed_job. I switched to DJ '2.1.2' and it's worked for me fine.

P.S.: Sorry for my English :)

like image 29
suhovius Avatar answered Oct 19 '22 02:10

suhovius