Advertisement
Guest User

Untitled

a guest
May 24th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. # app/models/my_worker.rb
  2. class MyWorker
  3. include Carbon::Worker
  4.  
  5. BoomError = Class.new(StandardError)
  6.  
  7. retry_without_notification BoomError
  8.  
  9. def call(attributes)
  10. raise BoomError
  11. end
  12. end
  13.  
  14. # lib/carbon/worker.rb
  15. module Carbon
  16. module Worker
  17. extend ActiveSupport::Concern
  18.  
  19. included do
  20. include Sidekiq::Worker
  21.  
  22. sidekiq_retries_exhausted do |_message, ex|
  23. Rails.logger.info "Sending exception, after final retry, to Sentry: #{ex.inspect}"
  24. ::Carbon::ExceptionHandler.capture_exception(ex.cause, message: ex.cause.message)
  25. end
  26.  
  27. class_attribute :errors_to_retry_without_notification
  28. end
  29.  
  30. class_methods do
  31. # macro to set exceptions which should be retried without notification to
  32. # exception monitoring.
  33. #
  34. def retry_without_notification(expections)
  35. self.errors_to_retry_without_notification = Array(expections)
  36. end
  37.  
  38. def method_added(m)
  39. if m == :perform
  40. raise "Do not implement #perform, use #call instead"
  41. end
  42. end
  43. end
  44.  
  45. class RetryableError < StandardError; end
  46.  
  47. def call(*args)
  48. raise NotImplementedError
  49. end
  50.  
  51. def perform(*args)
  52. call(*args)
  53. rescue *self.class.errors_to_retry_without_notification
  54. raise RetryableError # Raven is configured to ignore this exception
  55. end
  56. end
  57. end
  58.  
  59. # config/initializers/raven.rb
  60. Raven.configure do |config|
  61. config.excluded_exceptions << "Carbon::Worker::RetryableError"
  62. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement