0

in my app, DJ fires off a an email but for some reason I see the email in the log file twice though it is sent only once? I confirmed in the logs that DJ runs only once and user_mailer runs only once. So why do I see the email twice in the log file? What is Rails doing?

Rendered user_mailer/room_notification.text.erb (0.9ms)

Sent mail to [email protected] (1097ms)
Date: Fri, 30 Sep 2011 13:34:56 -0700
From: "roomxcom" <no-reply@roomxcom>
To: [email protected]
Message-ID: <[email protected]>
Subject: [Email Testing] Test 12
Mime-Version: 1.0
Content-Type: text/plain;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

AAAAA Did something:

Test 12

http://localhost:3000/c19

Sent mail to [email protected] (3510ms)
Date: Fri, 30 Sep 2011 13:34:56 -0700
From: "roomxcom" <no-reply@roomxcom>
To: [email protected]
Message-ID: <[email protected]>
Subject: [Email Testing] Test 12
Mime-Version: 1.0
Content-Type: text/plain;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

AAAAA Did something:

Test 12

http://localhost:3000/c19
  SQL (0.1ms)  BEGIN
  AREL (0.3ms)  DELETE FROM "delayed_jobs" WHERE "delayed_jobs"."id" = 25
  SQL (0.4ms)  COMMIT

Delayed Job:

UserMailer.delay.room_notification(room, record.user, room_member.user, record.item)

User Mailer:

def room_notification(room, user_creator, user_recipient, item)
  ...
mail( :from => "XXXX <[email protected]>",
      :to => user_recipient.email,
      :subject => "[#{@room.title}] #{@item.title}"
    ).deliver

Any idea what' going on and why rails is showing the email in the log twice? Thanks

2
  • May be because for the first time ur delayed job failed and then queued again and then in second attempt job was successful. Paste the output of rake jobs:work.
    – Mohit Jain
    Sep 30, 2011 at 22:18
  • Mohit, that's not it. DJ is empty instantly after this run. Sep 30, 2011 at 22:35

1 Answer 1

9

edit: Here's a clearer explanation now that it's daytime and I've had some coffee...

This line is adding the message to the queue:

UserMailer.delay.room_notification(room, record.user, room_member.user, record.item)

So Delayed_job then calls the room_notification method in UserMailer to send the mail:

def room_notification(room, user_creator, user_recipient, item)
  # ...
  mail(:from => "XXXX <[email protected]>",
       :to => user_recipient.email,
       :subject => "[#{@room.title}] #{@item.title}"
  ).deliver
end

This would be enough in itself to send the mail but you also have a redundant .deliver on the end of the mail method so it's sending it twice.

TL;DR Remove .deliver from mail(...) in the room_notification method and everything should be fine.

4
  • I uses delay to send to DJ. Then DJ delivers. Isn't that the way it's done? Sep 30, 2011 at 21:02
  • 1
    UserMailer.delay.room_notification(room, record.user, room_member.user, record.item) will add it to the DJ queue but you're also sending it immediately with mail(...).deliver.
    – Simon
    Sep 30, 2011 at 21:05
  • 1
    Simon. The user mailer isn't called until delayed job? So I'm not following? Sep 30, 2011 at 22:16
  • I've edited my answer so hopefully it should make more sense now :)
    – Simon
    Oct 1, 2011 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.