Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delayed_Job - handle_asynchronously with ActionMailer?

So I'm using Delayed Jobs and I'm trying to figure out how to get all of my mailers to be delayed. Right now, I've put handle_asynchronously on all of my action mailer methods… but I don't think that is going to work.

def first_notification(time)
  @time = time
  mail :to => time.person.email,
       :from => "[email protected]",
       :subject => "#{time.person.name} wants to say hi"
end
handle_asynchronously :advisor_first_notification, :priority => 20

The reason I don't think this is going to work is because I call it as such:

UserMailer.first_notification(@time).deliver

So how would it handle the .deliver part of this? Right now I get an exception.

EXCEPTION: #<ArgumentError: wrong number of arguments (1 for 0)>

Which makes me feel that something is getting messed up in the deliver aspect.

I would rather not have a separate job file for each email (as I have a lot of them), so what is the proper way to handle this?

The only other option I can think of is to encapsulate the calls into a method within my models and have them have the handle_asynchronously - that way they can call the entire thing at once.

like image 547
RyanJM Avatar asked May 22 '11 04:05

RyanJM


2 Answers

The mailer is a bit tricky... Instead of using the handle_asynchronously syntax:

UserMailer.delay.first_notification(@time)

The 'trick' is having delay() before the mailer method

like image 177
Jesse Wolgamott Avatar answered Nov 15 '22 09:11

Jesse Wolgamott


Further to Jesse's answer, the collectiveidea's fork of delayed_job indicates that you should definitely not use the deliver method at all with Rails 3 Mailer code:

# without delayed_job
Notifier.signup(@user).deliver
# with delayed_job
Notifier.delay.signup(@user)
like image 44
dznz Avatar answered Nov 15 '22 09:11

dznz