Rails:完全被delayed_job困扰。不接受任何论点

时间:2010-12-02 17:26:30

标签: ruby-on-rails delayed-job

我正在尝试使用collectiveidea的delayed_job在Rails 3应用程序中发送邮件。如果我尝试定期发送邮件(.deliver)它工作正常,但一旦我转到延迟工作,事情就会崩溃。

我在delayed_job mysql表中遇到的标准错误是:

  

{undefined method`name'for   零:NilClass ...

其中'name'是邮件程序视图中的第一个参数(@ contact.name)。如果我再次使用delayed_job,这样可以正常工作。

如果我删除视图中对@contact的所有引用,我会得到

  

{发件人(Return-Path,Sender或   From)需要发送消息

简而言之,似乎根本不理解任何论据。

以下所有相关代码 - 如果有人对此有任何建议,将非常感谢

CONTROLLER

  def sendmail
    @contact = Contact.new(params[:contact])
    if @contact.save
      ContactMailer.delay.contact_mail(@contact)
      flash[:notice] = 'Your message has been successfully sent'
      redirect_to root_path
    else
      render :action => 'index'
    end
  end

MAILER:

class ContactMailer < ActionMailer::Base
  default :from => "my@email.address"  

  def contact_mail(contact)
    @contact = contact
    mail(:to => 'my@email.address', :subject => 'Contact Form Query', :from => 'my@email.address', :content_type => 'text/plain')
  end
  handle_asynchronously :contact_mail, :run_at => Proc.new { 2.seconds.from_now }
end

MAILER VIEW:

You have received a new query:
-----------------------------------

<%= @contact.name %>

<%= @contact.business %>

<%= @contact.phone %>

-----------------------
<%= @contact.message %>
-----------------------

初​​始化:

Delayed::Worker.destroy_failed_jobs = false
Delayed::Worker.max_run_time = 5.weeks

最后,完整的错误消息:

    {undefined method `name' for nil:NilClass

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0/lib/active_support/whiny_nil.rb:48:in `method_missing'

/Users/john/Websites/Rails/InDevelopment/connectted/app/views/contact_mailer/contact_mail.html.erb:4:in `_app_views_contact_mailer_contact_mail_html_erb___3386534441642202773_2166008980__4301703149293725172'

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0/lib/action_view/template.rb:135:in `block in render'

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0/lib/active_support/notifications.rb:54:in `instrument'

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0/lib/action_view/template.rb:127:in `render'

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0/lib/action_view/render/rendering.rb:59:in `block in _render_template'

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0/lib/active_support/notifications.rb:52:in `block in instrument'

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0/lib/active_support/notifications/instrumenter.rb:21:in `instrument'

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0/lib/active_support/notifications.rb:52:in `instrument'

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0/lib/action_view/render/rendering.rb:56:in `_render_template'

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0/lib/action_view/render/rendering.rb:26:in `render'

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0/lib/abstract_controller/rendering.rb:114:in `_render_template'

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0/lib/abstract_controller/rendering.rb:108:in `render_to_body'

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0/lib/abstract_controller/rendering.rb:101:in `render_to_string'

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0/lib/abstract_controller/rendering.rb:92:in `render'

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionmailer-3.0.0/lib/action_mailer/deprecated_api.rb:111:in `render'

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionmailer-3.0.0/lib/action_mailer/old_api.rb:210:in `block in create_parts'

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionmailer-3.0.0/lib/action_mailer/old_api.rb:208:in `each'

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionmailer-3.0.0/lib/action_mailer/old_api.rb:208:in `create_parts'

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionmailer-3.0.0/lib/action_mailer/deprecated_api.rb:143:in `create_parts'

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionmailer-3.0.0/lib/action_mailer/old_api.rb:77:in `process'

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionmailer-3.0.0/lib/action_mailer/base.rb:446:in `process'

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionmailer-3.0.0/lib/action_mailer/base.rb:441:in `initialize'

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionmailer-3.0.0/lib/action_mailer/base.rb:425:in `new'

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionmailer-3.0.0/lib/action_mailer/base.rb:425:in `method_missing'

/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/performable_mailer.rb:6:in `perform'

/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/backend/base.rb:83:in `invoke_job'

/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/worker.rb:119:in `block (2 levels) in run'

/Users/john/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/timeout.rb:57:in `timeout'

/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed
/worker.rb:119:in `block in run'

/Users/john/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/benchmark.rb:309:in `realtime'

/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/worker.rb:118:in `run'

/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/worker.rb:176:in `reserve_and_run_one_job'

/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/worker.rb:103:in `block in work_off'

/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/worker.rb:102:in `times'

/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/worker.rb:102:in `work_off'

/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/worker.rb:77:in `block (2 levels) in start'

/Users/john/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/benchmark.rb:309:in `realtime'

/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/worker.rb:76:in `block in start'

/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/worker.rb:73:in `loop'

/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/worker.rb:73:in `start'

/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/command.rb:100:in `run'

/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/command.rb:79:in `block in run_process'

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/daemons-1.1.0/lib/daemons/application.rb:250:in `call'

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/daemons-1.1.0/lib/daemons/application.rb:250:in `block in start_proc'

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/daemons-1.1.0/lib/daemons/daemonize.rb:199:in `call'

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/daemons-1.1.0/lib/daemons/daemonize.rb:199:in `call_as_daemon'

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/daemons-1.1.0/lib/daemons/application.rb:254:in `start_proc'

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/daemons-1.1.0/lib/daemons/application.rb:294:in `start'

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/daemons-1.1.0/lib/daemons/controller.rb:70:in `run'

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/daemons-1.1.0/lib/daemons.rb:193:in `block in run_proc'

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/daemons-1.1.0/lib/daemons/cmdline.rb:112:in `call'

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/daemons-1.1.0/lib/daemons/cmdline.rb:112:in `catch_exceptions'

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/daemons-1.1.0/lib/daemons.rb:192:in `run_proc'

/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/command.rb:78:in `run_process'

/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/command.rb:72:in `block in daemonize'

/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/command.rb:70:in `times'

/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/command.rb:70:in `daemonize'
script/delayed_job:5:in `<main>' |

5 个答案:

答案 0 :(得分:11)

问题在于邮件中的代码是使用对象而不是对象id:

def contact_mail(contact)
    @contact = contact
    mail(:to => 'my@email.address', :subject => 'Contact Form Query', :from => 'my@email.address', :content_type => 'text/plain')
end

你应该改变它:

def contact_mail(contact_id)
    @contact = Contact.find(contact_id)
    mail(:to => 'my@email.address', :subject => 'Contact Form Query', :from => 'my@email.address', :content_type => 'text/plain')
end

切勿将实际对象传递给邮件程序。只需传递id并在处理时检索它们。

答案 1 :(得分:1)

我有同样的问题,并通过将所有属性移动到实例变量来修复它。所以,在视图中:

<%= @contact.name %>
<%= @contact.business %>
<%= @contact.phone %>

变为:

<%= @name %>
<%= @business %>
<%= @phone %>

我只是在邮件程序方法中查找了实例变量。所以,在邮件中:

def contact_mail(contact)
    @contact = contact
    mail(:to => 'my@email.address', :subject => 'Contact Form Query', :from => 'my@email.address', :content_type => 'text/plain')
end

变为:

def contact_mail(contact)
    @name = contact.name
    @business = contact.business
    @phone = contact.phone
    mail(:to => 'my@email.address', :subject => 'Contact Form Query', :from => 'my@email.address', :content_type => 'text/plain')
end

答案 2 :(得分:0)

为什么不起作用?不确定,我想它与一些ActionMailer伏都教有关。

如何让它运作?摘要将延迟的工作分成另一类。有延迟的方法只是发送电子邮件;这意味着电子邮件的呈现是实时完成的,但是它的发送被延迟了。这应该解决您的问题并完成电子邮件的异步发送。

答案 3 :(得分:0)

嘿,这可能很遥远,但你没有向你的MAILER发送任何参数。这是我保存在lib / dir中的referenced_email.rb的副本。我用delayed_job称呼它:

#my controller
Delayed::Job.enqueue ReferredEmail.new(params[:subject], params[:editor1])

#Reffered_Email.rb

class ReferredEmail < Struct.new(:subject, :editor1)
  def perform
    (CardReferral.all.map(&:email) - CardSignup.all.map(&:email)).each do |cf|
      @cf = CardReferral.find_by_email(cf)
      Notifier.deliver_referred_magic_email(User.find(@cf.user_id), @cf.email, @cf.name, @cf.message, subject, editor1)
    end
  end
end

答案 4 :(得分:0)

好像你正在使用两者:

  ContactMailer.delay.contact_mail(@contact)

  handle_asynchronously :contact_mail, :run_at => Proc.new { 2.seconds.from_now }

你应该只做一个或另一个。尝试删除handle_asynchronously并查看是否可以解决问题。