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

问题描述 投票:1回答:1

我的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

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

ruby-on-rails sidekiq
1个回答
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"
© www.soinside.com 2019 - 2024. All rights reserved.