Skip to content

Commit

Permalink
Proper ActiveJob integration.
Browse files Browse the repository at this point in the history
Close #709
Close #671
  • Loading branch information
nateberkopec committed Jul 16, 2017
1 parent 913f397 commit 2ab5755
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 23 deletions.
6 changes: 6 additions & 0 deletions lib/raven/integrations/rails.rb
Expand Up @@ -63,6 +63,12 @@ class Rails < ::Rails::Railtie
end
end

initializer 'raven.active_job' do
ActiveSupport.on_load :active_job do
require 'raven/integrations/rails/active_job'
end
end

rake_tasks do
require 'raven/integrations/tasks'
end
Expand Down
62 changes: 42 additions & 20 deletions lib/raven/integrations/rails/active_job.rb
@@ -1,31 +1,53 @@
module Raven
class Rails
module ActiveJob
module ActiveJobExtensions
ALREADY_SUPPORTED_SENTRY_ADAPTERS = %w(
ActiveJob::QueueAdapters::SidekiqAdapter
ActiveJob::QueueAdapters::DelayedJobAdapter
).freeze

def self.included(base)
base.class_eval do
rescue_from(Exception) do |exception|
# Do not capture exceptions when using Sidekiq so we don't capture
# The same exception twice.
unless self.class.queue_adapter.to_s == 'ActiveJob::QueueAdapters::SidekiqAdapter'
active_job_details = {
:active_job => self.class.name,
:arguments => arguments,
:scheduled_at => scheduled_at,
:job_id => job_id,
:locale => locale
}
around_perform do |job, block|
capture_and_reraise_with_sentry(job, block)
end
end
end

# Add provider_job_id details if Rails 5
if defined?(provider_job_id)
active_job_details[:provider_job_id] = provider_job_id
end
def capture_and_reraise_with_sentry(job, block)
block.call
rescue Exception => exception # rubocop:disable Lint/RescueException
return if already_supported_by_specific_integration?(job)
Raven.capture_exception(exception, :extra => raven_context(job))
raise exception
ensure
Context.clear!
BreadcrumbBuffer.clear!
end

Raven.capture_exception(exception, :extra => active_job_details)
raise exception
end
end
def already_supported_by_specific_integration?(job)
ALREADY_SUPPORTED_SENTRY_ADAPTERS.include?(job.class.queue_adapter.to_s)
end

def raven_context(job)
ctx = {
:active_job => job.class.name,
:arguments => job.arguments,
:scheduled_at => job.scheduled_at,
:job_id => job.job_id,
:locale => job.locale
}
# Add provider_job_id details if Rails 5
if job.respond_to?(:provider_job_id)
ctx[:provider_job_id] = job.provider_job_id
end

ctx
end
end
end
end

class ActiveJob::Base
include Raven::Rails::ActiveJobExtensions
end
47 changes: 47 additions & 0 deletions spec/raven/integrations/rails/activejob_spec.rb
@@ -0,0 +1,47 @@
require "spec_helper"
require "rspec/rails"
require "raven/transports/dummy"
require "raven/integrations/rails"
require 'raven/integrations/rails/active_job'

class MyActiveJob < ActiveJob::Base
self.queue_adapter = :inline
self.logger = nil

class TestError < RuntimeError
end

def perform
raise TestError, "Boom!"
end
end

describe MyActiveJob do
before(:each) do
Raven.client.transport.events = []
end

it "captures exceptions" do
job = MyActiveJob.new

expect { job.perform_now }.to raise_error(MyActiveJob::TestError)

expect(Raven.client.transport.events.size).to eq(1)
end

it "clears context" do
Raven.extra_context(:foo => :bar)
job = MyActiveJob.new

expect { job.perform_now }.to raise_error(MyActiveJob::TestError)
event = JSON.parse!(Raven.client.transport.events.first[1])

expect(event["extra"]["foo"]).to eq("bar")

Raven.client.transport.events = []
expect { job.perform_now }.to raise_error(MyActiveJob::TestError)
event = JSON.parse!(Raven.client.transport.events.first[1])

expect(event["extra"]["foo"]).to eq(nil)
end
end
5 changes: 2 additions & 3 deletions spec/support/test_rails_app/app.rb
@@ -1,10 +1,9 @@
require 'rails'
# require "active_model/railtie"
# require "active_job/railtie"
# require "active_record/railtie"
require "action_view/railtie"
require "action_controller/railtie"
# require "action_mailer/railtie"
require "action_view/railtie"
require "active_job/railtie"
# require "action_cable/engine"
# require "sprockets/railtie"
# require "rails/test_unit/railtie"
Expand Down

0 comments on commit 2ab5755

Please sign in to comment.