Skip to content

Instantly share code, notes, and snippets.

@krisleech
Created May 14, 2019 13:54
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 krisleech/2d3f8e5374fc5bcc77339d7da4e24296 to your computer and use it in GitHub Desktop.
Save krisleech/2d3f8e5374fc5bcc77339d7da4e24296 to your computer and use it in GitHub Desktop.
Worker (sidekiq) module to report exceptions only when last retry has failed
# app/models/my_worker.rb
class MyWorker
include Carbon::Worker
BoomError = Class.new(StandardError)
retry_without_notification BoomError
def call(attributes)
raise BoomError
end
end
# lib/carbon/worker.rb
module Carbon
module Worker
extend ActiveSupport::Concern
included do
include Sidekiq::Worker
sidekiq_retries_exhausted do |_message, ex|
Rails.logger.info "Sending exception, after final retry, to Sentry: #{ex.inspect}"
::Carbon::ExceptionHandler.capture_exception(ex.cause, message: ex.cause.message)
end
class_attribute :errors_to_retry_without_notification
end
class_methods do
# macro to set exceptions which should be retried without notification to
# exception monitoring.
#
def retry_without_notification(expections)
self.errors_to_retry_without_notification = Array(expections)
end
def method_added(m)
if m == :perform
raise "Do not implement #perform, use #call instead"
end
end
end
class RetryableError < StandardError; end
def call(*args)
raise NotImplementedError
end
def perform(*args)
call(*args)
rescue *self.class.errors_to_retry_without_notification
raise RetryableError # Raven is configured to ignore this exception
end
end
end
# config/initializers/raven.rb
Raven.configure do |config|
config.excluded_exceptions << "Carbon::Worker::RetryableError"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment