Skip to content

Instantly share code, notes, and snippets.

@henrik
Last active October 27, 2023 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henrik/f730db07e48c021082bcb4eb0875d604 to your computer and use it in GitHub Desktop.
Save henrik/f730db07e48c021082bcb4eb0875d604 to your computer and use it in GitHub Desktop.
Sidekiq "silence_errors_during_retries".
# Intended for silent Sidekiq retries: https://www.mikeperham.com/2017/09/29/retries-and-exceptions/
class Exception
attr_accessor :ignore_in_error_reporting
end
# Workers with `sidekiq_options(silence_errors_during_retries: [ Some, Errors ])` will retry silently on those errors, without reporting the errors to Honeybadger.
# If the job runs out of retries, the error will be reported then – see `death_handlers` below.
#
# Read more: https://www.mikeperham.com/2017/09/29/retries-and-exceptions/
class SidekiqServerMiddlewareSilenceErrorsDuringRetries
def call(worker, _msg, _queue)
errors_to_silence = worker.class.get_sidekiq_options["silence_errors_during_retries"] || []
# Capture most errors if set to true
errors_to_silence = [ StandardError ] if errors_to_silence == true
return yield if errors_to_silence.empty?
begin
yield
rescue *errors_to_silence => ex
ex.ignore_in_error_reporting = true
raise ex
end
end
end
Sidekiq.configure_server do |config|
config.server_middleware do |chain|
chain.add SidekiqServerMiddlewareSilenceErrorsDuringRetries
end
# For some jobs, we silently retry without reporting certain errors, such as network errors.
# But we do want to report those errors when all retries are exhausted, so we don't miss them entirely.
#
# Read more:
# * https://www.mikeperham.com/2017/09/29/retries-and-exceptions/
# * https://github.com/mperham/sidekiq/wiki/Error-Handling#death-notification
config.death_handlers << ->(job, ex) do
# Change this value so that the error handling can then report it.
ex.ignore_in_error_reporting = false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment