Search code examples
ruby-on-railsrubyexceptiondelayed-job

MissingTemplate exception raised in mailer using delayed job


I use delayed job to carry out background tasks in my app. It does some stuff and then sends a mail. This is an example of the code block preformed as a delayed job.

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

This has been working fine, I have a template in the correct directory some_template.text.plain.erb. I have started seeing this raise a MissingTemplate exception and this stops the delayed job and tries again after a given time (as expected when exceptions are raised in delayed job). Some cases this will fail 2 or 3 times and then deliver successfully, other cases there will be no exception raised. How can a template not exist then be found successfully upon the next attempt?

The error is looking for a template .erb is the plain.text.erb causing this problem. Any ideas why this is causing an intermittent problem? I have tried wrapping the sending of the mail in a rescue block as I would rather not send the mail than raise an exception.

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

But the exception is still raised. Does delayed job do anything that might stop this rescue block working?

Here is the stack trace

  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

I know there are a couple of problems here but any advice would be appreciated.


Solution

  • It's hard to say from just what you've posted but with Delayed Job Mailer errors I would typically check for two things:

    1. That your workers are being restarted when you deploy new code, so that they are using the latest version of your app.

    2. Are the arguments you're passing to the Mailer method used to retrieve records? If so be careful that records don't change or disappear between the job being created and the job being worked. Consider passing in exactly the attributes you need to build the mail if this is the case.