Rails |如何忽略引发错误 sidekiq worker |水豚

Rails | How to Ignore raise error sidekiq worker | Capybara

我是 rails 的新手,我的团队最近开始使用 sidekiq

在模型中使用此指令调用工人

CoolClassJob.perform_async(...)

我正在使用代码与此类似的工作器:

class CoolClassJob
  include Sidekiq::Worker
  sidekiq_options queue: "payment", retry: 5

  sidekiq_retry_in do |count|
    10
  end

  def perform()
    ...
        whatever = {...}

        if whatever.status == 'successful'
          thisCoolFunction                  # successfully ends job
        elsif whatever.status == 'failed'
          anotherCoolFunction               # successfully ends job
        elsif whatever.pending?             # I want to retry if it falls in this condition since it is "waiting" for another task to complete.
          raise 'We are trying again'
        end
    ...
  end
  ...
end

我试过

          begin
            raise 'We are trying again!'
          rescue
             nil
          end

但是当我 运行 我的测试时,我得到这个错误:

     Failure/Error: raise 'We are trying again!'
     
     RuntimeError:
       'We are trying again!'
     ...

这对我来说当然有意义,因为我提出了错误,所以我尝试搜索但无法提出解决方案。 我想知道它是否能够 a) retry 再次不引发错误或 b) 告诉 Capybara (rspec) 继续尝试而不抛出错误。

一种方法是重新安排您的工作人员:

def perform()
    ...
        whatever = {...}

        if whatever.status == 'successful'
          thisCoolFunction                  # successfully ends job
        elsif whatever.status == 'failed'
          anotherCoolFunction               # successfully ends job
        elsif whatever.pending?             # I want to retry if it falls in this condition since it is "waiting" for another task to complete.
          self.class.perform_in(1.minute)
        end
    ...
end

或者您可以检查这个 SO 答案:Sidekiq/Airbrake only post exception when retries extinguished