如何在不引发异常的情况下重试sidekiq worker

时间:2017-10-23 08:42:36

标签: ruby-on-rails sidekiq

我的sidekiq工作人员使用get请求获取语音识别结果,响应状态为" SUCCESS"或者" FAILED"或者" RUNNING"。当状态为" RUNNING"时,我想在10分钟内重试sidekiq worker。如何在没有睡眠的情况下重试或引发异常。因为睡眠会消耗太多资源而引发异常会留下我不想记录的新的错误日志。

class GetAsrTextWorker
  include Sidekiq::Worker
  sidekiq_options :queue => :default, :retry => 5

  sidekiq_retry_in do | count|
    600 * (count + 1)
  end

  def perform(task_id):
    # get request to get the outcome
    if status == "SUCCESSD"
       # save the outcome
    elsif status == "FAILED"
       raise AsrError
    elsif status == "RUNNING"
       raise "Asr running"
    end
  rescue AsrError => e
    Sidekiq.logger.error e.message
  end
end

此方法将重试此工作程序,但会导致我不想记录的newrelic中的丑陋错误日志。

1 个答案:

答案 0 :(得分:1)

使用the New Relic Ruby agent option error_collector.ignore_errors忽略您提出的具体例外情况:

定义您可以提出的自定义异常:

# lib/retry_job.rb
class RetryJob < StandardError; end

定义引发异常的worker:

# app/workers/foo_worker.rb
class FooWorker
  include Sidekiq::Worker
  def perform
    raise RetryJob
  end
end

使用New Relic代理忽略该异常:

# config/newrelic.yml
common: &default_settings
  error_collector:
    ignore_errors: "ActionController::RoutingError,Sinatra::NotFound,RetryJob"