开发者

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?


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜