diff --git a/CHANGELOG.md b/CHANGELOG.md index 199bc0e7b..c929270cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ +- Correct the timing of loading ActiveJobExtensions, take 2 [#1494](https://github.com/getsentry/sentry-ruby/pull/1494) + ## 4.6.0 ### Features diff --git a/sentry-rails/lib/sentry/rails/railtie.rb b/sentry-rails/lib/sentry/rails/railtie.rb index 5f4e63f97..4fc9afb16 100644 --- a/sentry-rails/lib/sentry/rails/railtie.rb +++ b/sentry-rails/lib/sentry/rails/railtie.rb @@ -16,7 +16,10 @@ class Railtie < ::Rails::Railtie # before the application is eager-loaded (before user's jobs register their own callbacks) # See https://github.com/getsentry/sentry-ruby/issues/1249#issuecomment-853871871 for the detail explanation initializer "sentry.extend_active_job", before: :eager_load! do |app| - extend_active_job if defined?(ActiveJob) + ActiveSupport.on_load(:active_job) do + require "sentry/rails/active_job" + prepend Sentry::Rails::ActiveJobExtensions + end end config.after_initialize do |app| @@ -45,11 +48,6 @@ def configure_trusted_proxies Sentry.configuration.trusted_proxies += Array(::Rails.application.config.action_dispatch.trusted_proxies) end - def extend_active_job - require "sentry/rails/active_job" - ActiveJob::Base.send(:prepend, Sentry::Rails::ActiveJobExtensions) - end - def extend_controller_methods require "sentry/rails/controller_methods" require "sentry/rails/controller_transaction" diff --git a/sentry-rails/spec/isolated/active_job_activation.rb b/sentry-rails/spec/isolated/active_job_activation.rb index f805fc3a3..a3a2a1f35 100644 --- a/sentry-rails/spec/isolated/active_job_activation.rb +++ b/sentry-rails/spec/isolated/active_job_activation.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true # for https://github.com/getsentry/sentry-ruby/issues/1249 -require "active_job" +require "active_job/railtie" require "active_support/all" require "sentry/rails" require "minitest/autorun" @@ -12,6 +12,11 @@ class TestApp < Rails::Application app = TestApp +# Simulate code from the application's init files in config/initializer +app.initializer :config_initializer do + Rails.application.config.active_job.queue_name = "bobo" +end + # to simulate jobs being load during the eager_load initializer app.initializer :eager_load! do Object.class_eval <<~CODE @@ -56,4 +61,8 @@ def test_the_extension_is_loaded_before_eager_load_is_called assert_match(/RESCUED/, log_result, "ApplicationJob's rescue_from should be called") refute_match(/I SHOULD NEVER BE EXECUTED/, log_result, "ErrorJob's around_perform should not be triggered") end + + def test_the_extension_doesnt_load_activejob_too_soon + assert_equal("bobo", ApplicationJob.queue_name) + end end