chell
chell

Reputation: 7866

update user table after successful delayed job email sent

I am a NOOB trying to work with delayed_job.

I want to update a User Model after the mail is successfully sent using delayed job.

Send email:

UserMailer.delay.welcome_email(user)

if mail sent successfully do the following:

User.update_attributes(:emailed =>  true)

How can I get a callback or trigger when the email is successfully sent?

Upvotes: 6

Views: 1456

Answers (1)

Simone Carletti
Simone Carletti

Reputation: 176472

You need to create a Job object instead of calling the #delay helper. You can use the success hook to execute the callback.

class WelcomeEmailJob < Struct.new(:user_id)
  def perform
    UserMailer.welcome_email(user)
  end

  def success(job)
    user.update_attribute(:emailed, true)
  end

  private

    def user
      @user ||= User.find(user_id)
    end
end

Delayed::Job.enqueue WelcomeEmailJob.new(user.id)

Upvotes: 10

Related Questions