hiveer
hiveer

Reputation: 749

Delayed_job didn't work with action_mailer

First, I define a mailer method

class UserMailer < Devise::Mailer  
  # customize mail format
  def user_feedback(opts={})
    @v = "Hello"
    @opts = opts
    mail(:to => "[email protected]", :bcc => ["[email protected]"],  :subject => "Test")
  end

  handle_asynchronously :user_feedback     
end

And, I call this method in a request

class MailsController < ApplicationController
  def feedback      
    options[:feedback_time] = Time.now.strftime("%Y%m%d %H:%M:%S")
    UserMailer.user_feedback(options).deliver
  end
end

Then, I start delay_job worker like this

 rake jobs:workoff

Here is the log

[Worker(host:localhost.localdomain pid:2809)] Starting job worker
[Worker(host:localhost.localdomain pid:2809)] Job Delayed::PerformableMethod (id=11) RUNNING
[Worker(host:localhost.localdomain pid:2809)] No more jobs available. Exiting

[Worker(host:localhost.localdomain pid:2208)] Starting job worker
[Worker(host:localhost.localdomain pid:2208)] Job Class#user_feedback (id=1) RUNNING
[Worker(host:localhost.localdomain pid:2208)] Job Class#user_feedback (id=1) COMPLETED after 0.0819
[Worker(host:localhost.localdomain pid:2208)] Job Delayed::PerformableMethod (id=2)    RUNNING
[Worker(host:localhost.localdomain pid:2208)] 1 jobs processed at 2.7340 j/s, 0 failed
[Worker(host:localhost.localdomain pid:2208)] No more jobs available. Exiting

All of these have done, but the mail is not sent!

Upvotes: 1

Views: 589

Answers (2)

hiveer
hiveer

Reputation: 749

Here, I think I need to share my experience of how to get the right answer

When we use handle_asynchronously for a method, it calls alias_method_chain to the method, just like the method 'user_feedback' in the question. Visit https://github.com/collectiveidea/delayed_job/blob/44edd9473adbc415a3e51ead60a63107a9ad41e4/lib/delayed/message_sending.rb for more details

When delayed_job worker going to deal with the delayed send email job, it call a dynamic method, like user_feedback_without_delay. And now comes the problem, if we didn't explicitly point out the mail template. It'll be trying to find a template like user_feedback_without_delay.html.erb.

So, what we should do is to give the template explicitly like this mail(:to => "[email protected]", :bcc => ["[email protected]"], :subject => "Test", template_name: 'user_feedback')

Upvotes: 0

John Naegle
John Naegle

Reputation: 8247

Don't call .deliver and add a .delay before the method you are calling on UserMailer

See this: https://github.com/collectiveidea/delayed_job#rails-3-mailers

For example:

UserMailer.delay.user_feedback(options)

Upvotes: 2

Related Questions