Skip to content

:solid_queue_recurring patch loads Active Record and Active Job before initialization ("was loaded before application initialization") #3237

Backlog

Description

@kylekeesling

Issue Description

With config.enabled_patches << :solid_queue_recurring (added in #3140), every boot with eager_load off makes Rails log its early-load-hook warning seven times, each with a full stack trace:

:active_record was loaded before application initialization.
Prematurely executing load hooks will slow down your boot time
and could cause conflicts with the load order of your application.

The other six are the same warning for :active_model_translation, :active_record_encryption, :active_model_secure_password, :active_record_postgresqladapter, :active_job and :active_job_arguments.

The cause is Sentry::Rails::SolidQueueRecurring.patch (sentry-rails/lib/sentry/rails/solid_queue_recurring.rb):

apply = proc do
  if defined?(::SolidQueue::RecurringTask) && !(::SolidQueue::RecurringTask < Sentry::Rails::SolidQueueRecurring::RecurringTask)
    ::SolidQueue::RecurringTask.prepend(Sentry::Rails::SolidQueueRecurring::RecurringTask)
  end
end
::Rails.application.reloader.to_prepare(&apply)

to_prepare callbacks run in the finisher, before Rails.application.initialized? is true. ::SolidQueue::RecurringTask < ... autoloads the model, which loads SolidQueue::Record (so Active Record) and RecurringTask::Arguments (so Active Job). Rails' guard_load_hooks reports each one.

This is the same class of problem as #2912, fixed for ActionController::Live in #3109.

It matters most in rake tasks: rails db:prepare as a deploy pre-step prints all seven stack traces on every deploy.

Reproduction Steps

  1. Rails app with solid_queue and sentry-rails, and config.enabled_patches << :solid_queue_recurring in Sentry.init.
  2. No other initializer touches Active Record or Active Job at boot.
  3. Run any rake task with eager loading off, e.g. bin/rails db:version.
  4. Check the log (log/development.log in development).

Expected Behavior

Enabling the patch doesn't load Active Record or Active Job before the app finishes initializing.

Actual Behavior

Seven "was loaded before application initialization" warnings, all with solid_queue_recurring.rb (the apply proc inside patch) in the stack.

One possible fix: the app's Zeitwerk loader owns SolidQueue::RecurringTask (it lives in the engine's app/models), so the prepend could run from a load callback instead of to_prepare. That fires when the model is first loaded and again after every reload:

::Rails.autoloaders.main.on_load("SolidQueue::RecurringTask") do |klass, _abspath|
  klass.prepend(Sentry::Rails::SolidQueueRecurring::RecurringTask) unless klass < Sentry::Rails::SolidQueueRecurring::RecurringTask
end

We tested this wiring in an app: with eager loading on and off, SolidQueue::RecurringTask and ActiveJob::Base carried the same modules as with the built-in patch, and the warnings went away.

Ruby Version

4.0.2

SDK Version

sentry-rails 7.2.0 (same code in 7.3.0 and on master)

Integration and Its Version

Rails 8.2.0.alpha (edge), solid_queue 1.7.2, Rake

Sentry Config

Sentry.init do |config|
  config.enabled_patches << :solid_queue_recurring
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions