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?
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)
精彩评论