使用延迟作业在邮件程序中引发MissingTemplate异常

时间:2012-12-20 13:07:27

标签: ruby-on-rails ruby exception exception-handling delayed-job

我使用延迟作业在我的应用中执行后台任务。它会做一些事情,然后发送邮件。这是作为延迟作业执行的代码块的示例。

def task
 # do stuff
 AppMailer.post.some_template(@variable)
end

这个工作正常,我在正确的目录some_template.text.plain.erb中有一个模板。我已经开始看到这会引发一个MissingTemplate异常,这会停止延迟作业并在给定时间后再次尝试(正如预期在延迟作业中引发异常时)。在某些情况下,这将失败2或3次,然后成功交付,其他​​情况不会引发异常。如何在下次尝试时成功找到模板不存在?

错误是寻找导致此问题的.erb plain.text.erb模板。有什么想法导致间歇性问题吗?我已经尝试将邮件发送到救援区,因为我宁愿不发送邮件而不是提出例外。

def task
 # do stuff
 begin
 AppMailer.post.some_template(@variable)
 rescue ActionView::MissingTemplate => e
 # do stuff
 end
end

但仍有异常。延迟工作是否可以阻止此救援块的工作?

这是堆栈跟踪

  Error:
    Missing template app_mailer/some_template.erb in view path app/views:vendor/plugins/tolk/app/views
    /data/app/shared/bundled_gems/ruby/1.8/gems/actionpack-2.3.14/lib/action_view/paths.rb:74:in `find_template'
    /data/app/shared/bundled_gems/ruby/1.8/gems/actionpack-2.3.14/lib/action_view/base.rb:264:in `render'
    /data/app/shared/bundled_gems/ruby/1.8/gems/actionmailer-2.3.14/lib/action_mailer/base.rb:600:in `render'
    /data/app/shared/bundled_gems/ruby/1.8/gems/actionmailer-2.3.14/lib/action_mailer/base.rb:586:in `render_message'
    /data/app/shared/bundled_gems/ruby/1.8/gems/actionmailer-2.3.14/lib/action_mailer/base.rb:526:in `create!'
    /data/app/shared/bundled_gems/ruby/1.8/gems/actionmailer-2.3.14/lib/action_mailer/base.rb:485:in `initialize'
    /data/app/shared/bundled_gems/ruby/1.8/gems/actionmailer-2.3.14/lib/action_mailer/base.rb:427:in `new'
    /data/app/shared/bundled_gems/ruby/1.8/gems/actionmailer-2.3.14/lib/action_mailer/base.rb:427:in `method_missing'
    /data/app/releases/20121127213524/app/models/mail_delivery.rb:55:in `send'
    /data/app/releases/20121127213524/app/models/mail_delivery.rb:55:in `serialize_mail'
    /data/app/shared/bundled_gems/ruby/1.8/gems/activesupport-2.3.14/lib/active_support/callbacks.rb:178:in `send'
    /data/app/shared/bundled_gems/ruby/1.8/gems/activesupport-2.3.14/lib/active_support/callbacks.rb:178:in `evaluate_method'
    /data/app/shared/bundled_gems/ruby/1.8/gems/activesupport-2.3.14/lib/active_support/callbacks.rb:166:in `call'
    /data/app/shared/bundled_gems/ruby/1.8/gems/activesupport-2.3.14/lib/active_support/callbacks.rb:93:in `run'
    /data/app/shared/bundled_gems/ruby/1.8/gems/activesupport-2.3.14/lib/active_support/callbacks.rb:92:in `each'
    /data/app/shared/bundled_gems/ruby/1.8/gems/activesupport-2.3.14/lib/active_support/callbacks.rb:92:in `send'
    /data/app/shared/bundled_gems/ruby/1.8/gems/activesupport-2.3.14/lib/active_support/callbacks.rb:92:in `run'
    /data/app/shared/bundled_gems/ruby/1.8/gems/activesupport-2.3.14/lib/active_support/callbacks.rb:276:in `run_callbacks'
    /data/app/shared/bundled_gems/ruby/1.8/gems/activerecord-2.3.14/lib/active_record/callbacks.rb:344:in `callback'
    /data/app/shared/bundled_gems/ruby/1.8/gems/activerecord-2.3.14/lib/active_record/callbacks.rb:265:in `create'
    /data/app/shared/bundled_gems/ruby/1.8/gems/activerecord-2.3.14/lib/active_record/base.rb:2927:in `create_or_update_without_callbacks'
    /data/app/shared/bundled_gems/ruby/1.8/gems/activerecord-2.3.14/lib/active_record/callbacks.rb:250:in `create_or_update'
    /data/app/shared/bundled_gems/ruby/1.8/gems/activerecord-2.3.14/lib/active_record/base.rb:2577:in `save_without_validation'
    /data/app/shared/bundled_gems/ruby/1.8/gems/activerecord-2.3.14/lib/active_record/validations.rb:1089:in `save_without_dirty'
    /data/app/shared/bundled_gems/ruby/1.8/gems/activerecord-2.3.14/lib/active_record/dirty.rb:79:in `save_without_transactions'
    /data/app/shared/bundled_gems/ruby/1.8/gems/activerecord-2.3.14/lib/active_record/transactions.rb:229:in `send'
    /data/app/shared/bundled_gems/ruby/1.8/gems/activerecord-2.3.14/lib/active_record/transactions.rb:229:in `with_transaction_returning_status'
    /data/app/shared/bundled_gems/ruby/1.8/gems/activerecord-2.3.14/lib/active_record/connection_adapters/abstract/database_statements.rb:136:in `transaction'
    /data/app/shared/bundled_gems/ruby/1.8/gems/activerecord-2.3.14/lib/active_record/transactions.rb:182:in `transaction_without_trace_ActiveRecord_self_name_transaction'
    /data/app/shared/bundled_gems/ruby/1.8/gems/newrelic_rpm-3.4.0.1/lib/new_relic/agent/method_tracer.rb:491:in `transaction'
    /data/app/shared/bundled_gems/ruby/1.8/gems/newrelic_rpm-3.4.0.1/lib/new_relic/agent/method_tracer.rb:242:in `trace_execution_scoped'
    /data/app/shared/bundled_gems/ruby/1.8/gems/newrelic_rpm-3.4.0.1/lib/new_relic/agent/method_tracer.rb:486:in `transaction'
    /data/app/shared/bundled_gems/ruby/1.8/gems/activerecord-2.3.14/lib/active_record/transactions.rb:228:in `with_transaction_returning_status'
    /data/app/shared/bundled_gems/ruby/1.8/gems/activerecord-2.3.14/lib/active_record/transactions.rb:196:in `save'
    /data/app/shared/bundled_gems/ruby/1.8/gems/activerecord-2.3.14/lib/active_record/transactions.rb:208:in `rollback_active_record_state!'
    /data/app/shared/bundled_gems/ruby/1.8/gems/activerecord-2.3.14/lib/active_record/transactions.rb:196:in `save'
    /data/app/shared/bundled_gems/ruby/1.8/gems/activerecord-2.3.14/lib/active_record/base.rb:727:in `create'
    /data/app/releases/20121127213524/app/models/mail_delivery.rb:88:in `method_missing'
    /data/app/releases/20121127213524/lib/job_manifest.rb:670:in `notify_user_of_upload_without_audit'
    /data/app/releases/20121127213524/lib/job_manifest.rb:6:in `send'
    /data/app/releases/20121127213524/lib/job_manifest.rb:6:in `notify_user_of_upload_with_audit'
    /data/app/releases/20121127213524/lib/job_manifests/app_send_job.rb:43:in `notify_user_of_upload'
    /data/app/releases/20121127213524/lib/job_manifests/app_send_job.rb:43:in `extended'
    /data/app/releases/20121127213524/lib/job_manifest.rb:102:in `preflight_completed_changed_delayed'
    /data/app/shared/bundled_gems/ruby/1.8/gems/delayed_job-2.0.7/lib/delayed/performable_method.rb:35:in `send'
    /data/app/shared/bundled_gems/ruby/1.8/gems/delayed_job-2.0.7/lib/delayed/performable_method.rb:35:in `perform'
    /data/app/shared/bundled_gems/ruby/1.8/gems/delayed_job-2.0.7/lib/delayed/backend/base.rb:74:in `invoke_job'
    /data/app/shared/bundled_gems/ruby/1.8/gems/delayed_job-2.0.7/lib/delayed/worker.rb:122:in `run'
    /usr/lib64/ruby/1.8/timeout.rb:67:in `timeout'
    /data/app/shared/bundled_gems/ruby/1.8/gems/delayed_job-2.0.7/lib/delayed/worker.rb:122:in `run'
    /usr/lib64/ruby/1.8/benchmark.rb:308:in `realtime'
    /data/app/shared/bundled_gems/ruby/1.8/gems/delayed_job-2.0.7/lib/delayed/worker.rb:121:in `run'
    /data/app/shared/bundled_gems/ruby/1.8/gems/delayed_job-2.0.7/lib/delayed/worker.rb:173:in `reserve_and_run_one_job'
    /data/app/shared/bundled_gems/ruby/1.8/gems/delayed_job-2.0.7/lib/delayed/worker.rb:106:in `work_off'
    /data/app/shared/bundled_gems/ruby/1.8/gems/delayed_job-2.0.7/lib/delayed/worker.rb:105:in `times'
    /data/app/shared/bundled_gems/ruby/1.8/gems/delayed_job-2.0.7/lib/delayed/worker.rb:105:in `work_off'
    /data/app/shared/bundled_gems/ruby/1.8/gems/delayed_job-2.0.7/lib/delayed/worker.rb:80:in `start'
    /usr/lib64/ruby/1.8/benchmark.rb:308:in `realtime'
    /data/app/shared/bundled_gems/ruby/1.8/gems/delayed_job-2.0.7/lib/delayed/worker.rb:79:in `start'
    /data/app/shared/bundled_gems/ruby/1.8/gems/delayed_job-2.0.7/lib/delayed/worker.rb:76:in `loop'
    /data/app/shared/bundled_gems/ruby/1.8/gems/delayed_job-2.0.7/lib/delayed/worker.rb:76:in `start'
    /data/app/shared/bundled_gems/ruby/1.8/gems/delayed_job-2.0.7/lib/delayed/command.rb:100:in `run'
    (eval):1
    /data/app/current/script/runner:3:in `eval'
    /data/app/shared/bundled_gems/ruby/1.8/gems/rails-2.3.14/lib/commands/runner.rb:48
    /data/app/current/script/runner:3:in `require'
    /data/app/current/script/runner:3

我知道这里有一些问题,但任何建议都会受到赞赏。

1 个答案:

答案 0 :(得分:2)

很难说你发布了什么但是由于延迟工作邮件错误,我通常会检查两件事:

  1. 在部署新代码时,您的工作人员正在重新启动,以便他们使用最新版本的应用程序。

  2. 您传递给Mailer方法的参数是否用于检索记录?如果是这样,请注意记录不会在创建的作业和正在工作的作业之间发生变化或消失。如果是这种情况,请考虑准确传递构建邮件所需的属性。