简体   繁体   English

更多高级控制延迟作业工作人员重试

[英]More Advanced Control Over Delayed Job workers retry

So I'm using Delayed::Job workers (on Heroku) as an after_create callback after a user creates a certain model. 所以我在用户创建某个模型后使用Delayed :: Job worker(在Heroku上)作为after_create回调。

A common use case, though, it turns out, is for users to create something, then immediately delete it (likely because they made a mistake or something). 但事实证明,一个常见的用例是用户创建一些东西,然后立即删除它(可能是因为他们犯了错误或其他东西)。

When this occurs the workers are fired up, but by the time they query for the model at hand, it's already deleted, BUT because of the auto-retry feature, this ill-fated job will retry 25 times, and definitely never work. 当发生这种情况时,工作人员会被激活,但是当他们查询手头的模型时,它已经被删除了,但是由于自动重试功能,这个命中注定的工作将重试25次,并且绝对无法工作。

Is there any way I can catch certain errors and, when they occur, prevent that specific job from ever retrying again, but if it's not that error, it will retry in the future? 有什么方法可以捕获某些错误,并且当它们发生时,阻止该特定作业再次重试,但如果不是那个错误,它将来会重试吗?

Abstract the checks into the function you call with delayed_job. 摘要检查您使用delayed_job调用的函数。 Make the relevant checks wether your desired job can proceed or not and either work on that job or return success. 进行相关检查,以确定您的工作是否可以继续进行,并且可以完成该工作或返回成功。

To expand on David's answer, instead of doing this: 要扩展David的答案,而不是这样做:

def after_create
   self.send_later :spam_this_user
end

I'd do this: 我这样做:

# user.rb

def after_create
   Delayed::Job.enqueue SendWelcomeEmailJob.new(self.id)
end

# send_welcome_email_job.rb

class SendWelcomeEmailJob <  Struct(:user_id)
   def perform
      user = User.find_by_id(self.user_id)
      return if user.nil?  #user must have been deleted

      # do stuff with user
   end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM