Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 986
chell Avatar asked Aug 24 '11 09:08

chell


1 Answers

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)
like image 74
Simone Carletti Avatar answered Sep 28 '22 08:09

Simone Carletti