Ruby On Rails:Sidekiq:实例变量@organisation在邮件布局中不可用

时间:2015-03-24 09:17:31

标签: ruby-on-rails ruby redis sidekiq

我使用sidekiqredis-server在后​​台发送电子邮件。
问题:使用同步方法发送电子邮件时可以。即 在applicants_controller.rb

UserMailer.notify_applicant_assignment(current_assigned_user.id, applicant, workflow_step).deliver

但是,当我使用delay方法发送电子邮件时,即

applicants_controller.rb

中的

UserMailer.delay.notify_applicant_assignment(current_assigned_user.id, applicant, workflow_step)   

我在undefined method 'background_color' for nil:NilClass

中收到以下错误/layouts/user_mailer.html.erb:17:

mailers/user_mailer.rb

内的代码
class UserMailer < ActionMailer::Base
  include Sidekiq::Worker
  default from: CommonConstants::DO_NOT_REPLY_ADDRESS
  layout 'user_mailer'
    def notify_applicant_assignment(user_id, applicant_id, workflow_step_id)
       @user = User.find(user_id)
       @organization = @user.organization
       @applicant = Applicant.find(applicant_id)
       @url = root_url + 'applicants/' + @applicant.id.to_s
       @workflow_step = WorkflowStep.find(workflow_step_id)
       mail(to: @user.email, subject: 'Applicant Assigned.')
    end
end

layouts/user_mailer.html.erb

内的代码
<body style="background:#f4f4f4;">
<table width="100%" bgcolor="<%= @organization.background_color %>" cellpadding="0" cellspacing="0">
  <tr>
    <td>
      <table align="center" width="730" cellpadding="0" cellspacing="0" >

我在sidekiq控制台中遇到错误

2015-03-24T08:58:14Z 5595 TID-cjors WARN: {"retry"=>true, "queue"=>"default", "class"=>"Sidekiq::Extensions::DelayedMailer", "args"=>["---\n- !ruby/class 'UserMailer'\n- :notify_applicant_assignment\n- - 4\n  - '9'\n  - '9'\n"], "jid"=>"4421abf04e7e6864c7ee9fd8", "enqueued_at"=>1427187124.323067, "error_message"=>"undefined method `background_color' for nil:NilClass", "error_class"=>"ActionView::Template::Error", "failed_at"=>1427187124.3466575, "retry_count"=>4, "retried_at"=>1427187494.9246943}
2015-03-24T08:58:14Z 5595 TID-cjors WARN: undefined method `background_color' for nil:NilClass
2015-03-24T08:58:14Z 5595 TID-cjors WARN: /home/leapfrog/projects/ATS/app/views/layouts/user_mailer.html.erb:17:in `_app_views_layouts_user_mailer_html_erb__235114594899105140_70586860'
/home/leapfrog/.rvm/gems/ruby-2.1.1/gems/actionpack-4.0.4/lib/action_view/template.rb:143:in `block in render'
/home/leapfrog/.rvm/gems/ruby-2.1.1/gems/activesupport-4.0.4/lib/active_support/notifications.rb:161:in `instrument'
/home/leapfrog/.rvm/gems/ruby-2.1.1/gems/actionpack-4.0.4/lib/action_view/template.rb:141:in `render'
/home/leapfrog/.rvm/gems/ruby-2.1.1/gems/actionpack-4.0.4/lib/action_view/renderer/template_renderer.rb:61:in `render_with_layout'
/

1 个答案:

答案 0 :(得分:3)

延迟扩展的工作方式可能与您预期的不同。它们与常规异步作业不同。

the documentation中,明确指出:

  

我强烈建议避免在实例上延迟方法。这会将对象状态存储在Redis中,这可能会过时,从而导致过时的数据问题。

如果延迟扩展存储未渲染的模板和邮件参数(主题,来自,等等)而不是为过时的实例调用方法本身,我不会感到惊讶。

此外,请注意notify_applicant_assignment未被定义为方法(它看起来像一个实例方法),尽管Sidekiq documentation表示延迟应该适用于课堂方法。

  

任何方法都可以通过与上述相同的方法延迟:

我建议您使用notify_applicant_assignment类方法并重试(请注意def self.notify_applicant_assignmentdef notify_applicant_assignment中的自我。)

class UserMailer < ActionMailer::Base
  include Sidekiq::Worker
  default from: CommonConstants::DO_NOT_REPLY_ADDRESS
  layout 'user_mailer'
    def self.notify_applicant_assignment(user_id, applicant_id, workflow_step_id)
       @user = User.find(user_id)
       @organization = @user.organization
       @applicant = Applicant.find(applicant_id)
       @url = root_url + 'applicants/' + @applicant.id.to_s
       @workflow_step = WorkflowStep.find(workflow_step_id)
       mail(to: @user.email, subject: 'Applicant Assigned.')
    end
end

或者,我建议在applicants_controller.rb中使用标准异步引擎:

UserMailer.notify_applicant_assignment_async(current_assigned_user.id, applicant, workflow_step)

请让我知道它是怎么回事,所以如果还有需要,我可以再研究一下。

祝你好运!