ruby-on-rails - 如何处理 'record does not exist'错误并防止后台作业尝试重试?

标签 ruby-on-rails error-handling rails-activejob

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/

相关文章:

mysql - Rails mysql : How to query tables with deeply nested relations(has_many and belongs_to)?

javascript - 当严格模式为全局时,如何在浏览器JS中启用Rollbar?

objective-c - 如何跨线程跟踪NSError对象?

php - php error_reporting-禁用然后分别启用0和1无效

ruby-on-rails - 如何使用 Rspec 检查 ActiveJob 中排队的内容

ruby-on-rails - Rails 4 actionmailer 不发送电子邮件

ruby-on-rails - 使用 Capybara 和 Selenium 进行记录删除测试失败

ruby-on-rails-5 - Rails 5 渲染 View /部分带有演示者的事件作业

ruby-on-rails - 将 Sucker Punch 与 Active Job 一起使用,有没有办法取消排队的作业?

ruby-on-rails - Rails 路径助手在 js.coffee.erb 中不起作用