chell
chell

Reputation: 7866

delayed_job not sending emails but processes job

I am using the code example from delayed_job to send out a newsletter:

class NewsletterJob < Struct.new(:subscribers)
  def perform
    subscribers.each { |subscriber| NewsletterMailer.newsletter(subscriber.id) }
  end
end

Then I set the job here:

Delayed::Job.enqueue NewsletterJob.new(Subscriber.find(:all))

If I don't use delayed job the mail gets sent so it work. If I use delayed job directly the mail gets sent as follows:

NewsletterMailer.delay.newsletter(subscriber)

In the jobs table in the database the following is the yaml:

--- !ruby/struct:NewsletterJob
subscribers:
- !ruby/ActiveRecord:Subscriber
  attributes:
    id: '54'
    email: [email protected]
    created_at: '2013-08-09 04:44:51.113258'
    updated_at: '2013-08-09 08:26:05.934564'
    token: quVI0dhxyyentB7TJ1IO6w
- !ruby/ActiveRecord:Subscriber
  attributes:
    id: '56'
    email: [email protected]
    created_at: '2013-08-11 09:29:22.000829'
    updated_at: '2013-08-11 09:29:22.000829'
    token: a-n-yijwi38_HvGFSmetmA

I use a MockSMTP to get emails on my local machine.

For some reason even if the works seem to process the job the emails are not being sent.

Is there something wrong with the yaml?

Any assistance appreciated I'm a NOOB

Upvotes: 1

Views: 1651

Answers (2)

chell
chell

Reputation: 7866

In the end it turns that I needed to name the job class using camel case and put it in the lib directory.

#lib
NewsletterJob.rb

Then in #application.rb I had to turn off the auto deleting of failed jobs to see what was going on and also preload the file from the lib

Delayed::Worker.destroy_failed_jobs = false
require "#{Rails.root.to_s}/lib/NewsletterJob.rb"

Its all based on the answer here Delayed_job not executing the perform method but emptying the job queue

Upvotes: 0

Chris Peters
Chris Peters

Reputation: 18090

When you chain delayed in front of a mailer method, you don't need to call deliver on it. Delayed::Job takes care of that for you.

NewsletterMailer.delay.newsletter(subscriber) # No need to call `deliver` here

But when you call it from your own custom job struct, you need to remember to call the mailer's deliver method:

subscribers.each do |subscriber|
  NewsletterMailer.newsletter(subscriber.id).deliver # You need `deliver` here
end

Upvotes: 2

Related Questions