简体   繁体   English

Rails 3 - Delayed_Job

[英]Rails 3 - Delayed_Job

I'm working to learn how to user delayed_job on my rails 3 + heroku app. 我正在努力学习如何在我的rails 3 + heroku app上使用delayed_job。

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:

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):" 但是这个错误:“ArgumentError(无法排队不响应执行的项目):”

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? 任何delayed_job大师在那里? Thanks 谢谢

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

Your second method was correct which removes the .deliver method: 你的第二种方法是正确的,它删除了.deliver方法:

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

If you are getting an undefined method delay did you add DelayedJob to the Gemfile? 如果你得到一个未定义的方法delay你是否将DelayedJob添加到Gemfile?

gem "delayed_job"

Since including the delayed_job will add the "delay" method to everything. 由于包含delayed_job会将“延迟”方法添加到所有内容中。

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. 通常我会从after_save回调中触发我的工作。

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 在Rails 2中,我将它们放在lib /中如果你使用rails 3,你需要改变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/**/"]

I just had a look at the documentation, it's been a while since I actually used delayed_job... 我刚看了一下文档,我已经有一段时间了,因为我实际上使用了delayed_job ...

Jobs are Ruby objects with a method called perform , so you'd need enqueue an object which does 作业是带有一个名为perform的方法的Ruby对象,所以你需要将一个对象排入队列

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

in its perform method. 在其perform方法中。

Alternatively, you can use send_later : 或者,您可以使用send_later

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM