X. Wang
X. Wang

Reputation: 991

how to retry a sidekiq worker without raising a exception

My sidekiq worker use get request to get the speech recognition outcome, the response status will be "SUCCESS" or "FAILED" or "RUNNING". when the status is "RUNNING", I want to retry the sidekiq worker in 10 minutes. How can I retry without sleep or raise a exception. Because sleep will consume too much resource and raise exception will leave newrelic error log which I don't want to record.

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

This method will retry this worker but will lead to ugly error log in newrelic which I don't want to record.

Upvotes: 3

Views: 2075

Answers (1)

anothermh
anothermh

Reputation: 10546

Use the New Relic Ruby agent option error_collector.ignore_errors to ignore the specific exception you are raising:

Define a custom exception you can raise:

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

Define a worker that raises the exception:

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

Ignore that exception with the New Relic agent:

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

Upvotes: 5

Related Questions