Rails应用程序具有一个后台作业,该作业调用如下服务对象
class ObjectUpdateJob < ActiveJob::Base
queue_as :default
def perform(object_id)
ObjectUpdater.call(object_id)
end
end
class ObjectUpdater
def self.call(*args)
if @object = Object.find( args[:object_id] )
#... update some stuff from external services
else
#... raise an error and prevent the background job from attempting to retry
end
end
end
这可能是一个简单的问题,但是我在错误处理方面的经验有限,对此我不胜感激。
处理此“记录不存在”情况的“跟踪方式”是什么,并确保通知了后台作业以便不尝试重试?
最佳答案
您可以在工作人员的perform
操作中添加一个保护子句:
class ObjectUpdateJob < ActiveJob::Base
queue_as :default
def perform(object_id)
return unless Object.find(object_id)
ObjectUpdater.call(object_id)
end
end
但是为了获得更多保护,您可以对工作电话进行检查:
ObjectUpdateJob.perform_now(object_id) if Object.find(object_id)
这样,您就不会处理不存在的对象。
关于ruby-on-rails - 如何处理 'record does not exist'错误并防止后台作业尝试重试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35237639/