NoMethodError with delayed_job (collectiveidea gem)

心已入冬 提交于 2019-12-05 00:49:21

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 :)

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.

Just a quick note. I had this issue and it was because I was passing a variable from the model into the deliver method BEFORE_SAVE. Switching to AFTER_CREATE fixed the issue for me.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!