Class: Bugsnag::Sidekiq

Inherits:
Object
  • Object
show all
Defined in:
lib/bugsnag/integrations/sidekiq.rb

Overview

Extracts and attaches Sidekiq job and queue information to an error report

Constant Summary collapse

FRAMEWORK_ATTRIBUTES =
{
  :framework => "Sidekiq"
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSidekiq

Returns a new instance of Sidekiq.


14
15
16
17
18
# File 'lib/bugsnag/integrations/sidekiq.rb', line 14

def initialize
  Bugsnag.configuration.internal_middleware.use(Bugsnag::Middleware::Sidekiq)
  Bugsnag.configuration.detected_app_type = "sidekiq"
  Bugsnag.configuration.runtime_versions["sidekiq"] = ::Sidekiq::VERSION
end

Class Method Details

.configure_server(server) ⇒ Object


50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/bugsnag/integrations/sidekiq.rb', line 50

def self.configure_server(server)
  if Bugsnag::Sidekiq.sidekiq_supports_error_handlers
    server.error_handlers << proc do |ex, _context|
      Bugsnag::Sidekiq.notify(ex)
      Bugsnag.configuration.clear_request_data
    end
  end

  server.server_middleware do |chain|
    chain.add ::Bugsnag::Sidekiq
  end
end

.notify(exception) ⇒ Object


36
37
38
39
40
41
42
43
44
# File 'lib/bugsnag/integrations/sidekiq.rb', line 36

def self.notify(exception)
  Bugsnag.notify(exception, true) do |report|
    report.severity = "error"
    report.severity_reason = {
      :type => Bugsnag::Report::UNHANDLED_EXCEPTION_MIDDLEWARE,
      :attributes => FRAMEWORK_ATTRIBUTES
    }
  end
end

.sidekiq_supports_error_handlersObject


46
47
48
# File 'lib/bugsnag/integrations/sidekiq.rb', line 46

def self.sidekiq_supports_error_handlers
  Gem::Version.new(::Sidekiq::VERSION) >= Gem::Version.new('3.0.0')
end

Instance Method Details

#call(worker, msg, queue) ⇒ Object


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/bugsnag/integrations/sidekiq.rb', line 20

def call(worker, msg, queue)
  # store msg/queue in thread local state to be read by Bugsnag::Middleware::Sidekiq
  Bugsnag.configuration.set_request_data(:sidekiq, { :msg => msg, :queue => queue })
  error_raised = false
  yield
rescue Exception => ex
  error_raised = true
  self.class.notify(ex) unless self.class.sidekiq_supports_error_handlers
  raise
ensure
  # if an error was raised and error handlers are installed, the data will be cleared after
  # the notification is sent. Otherwise, the data must be cleared.
  keep_data = error_raised && self.class.sidekiq_supports_error_handlers
  Bugsnag.configuration.clear_request_data unless keep_data
end