From 13e6517b1cd11428ed6de0d32e1857c33f731b91 Mon Sep 17 00:00:00 2001 From: zzak Date: Fri, 1 Nov 2024 14:28:39 +0900 Subject: [PATCH] Fix circular require from Rails AJ adapter Similar to sidekiq/sidekiq#6477. This resulted in a circular require, which will fail in Rails CI. ``` CI=1 bundle exec rake test:integration:resque /home/zzak/.rbenv/versions/3.3.5/bin/ruby -w -I"lib:test" /home/zzak/.rbenv/versions/3.3.5/lib/ruby/gems/3.3.0/gems/rake-13.2.1/lib/rake/rake_test_loader.rb "test/integration/queuing_test.rb" Using resque /home/zzak/.rbenv/versions/3.3.5/lib/ruby/3.3.0/bundled_gems.rb:75: warning: /home/zzak/.rbenv/versions/3.3.5/lib/ruby/3.3.0/bundled_gems.rb:75: warning: loading in progress, circular require considered harmful - /home/zzak/code/rails/activejob/lib/active_job/queue_adapters/resque_adapter.rb from /home/zzak/.rbenv/versions/3.3.5/lib/ruby/gems/3.3.0/gems/rake-13.2.1/lib/rake/rake_test_loader.rb:6:in `
' from /home/zzak/.rbenv/versions/3.3.5/lib/ruby/gems/3.3.0/gems/rake-13.2.1/lib/rake/rake_test_loader.rb:6:in `select' from /home/zzak/.rbenv/versions/3.3.5/lib/ruby/gems/3.3.0/gems/rake-13.2.1/lib/rake/rake_test_loader.rb:21:in `block in
' from /home/zzak/.rbenv/versions/3.3.5/lib/ruby/3.3.0/bundled_gems.rb:75:in `block (2 levels) in replace_require' from /home/zzak/.rbenv/versions/3.3.5/lib/ruby/3.3.0/bundled_gems.rb:75:in `require' from /home/zzak/code/rails/activejob/test/integration/queuing_test.rb:3:in `' from /home/zzak/.rbenv/versions/3.3.5/lib/ruby/3.3.0/bundled_gems.rb:75:in `block (2 levels) in replace_require' from /home/zzak/.rbenv/versions/3.3.5/lib/ruby/3.3.0/bundled_gems.rb:75:in `require' from /home/zzak/code/rails/activejob/test/helper.rb:13:in `' from /home/zzak/.rbenv/versions/3.3.5/lib/ruby/3.3.0/bundled_gems.rb:75:in `block (2 levels) in replace_require' from /home/zzak/.rbenv/versions/3.3.5/lib/ruby/3.3.0/bundled_gems.rb:75:in `require' from /home/zzak/code/rails/activejob/test/support/integration/helper.rb:16:in `' from /home/zzak/.rbenv/versions/3.3.5/lib/ruby/3.3.0/bundled_gems.rb:75:in `block (2 levels) in replace_require' from /home/zzak/.rbenv/versions/3.3.5/lib/ruby/3.3.0/bundled_gems.rb:75:in `require' from /tmp/d20241101-2349270-pkyv1o/dummy/config/environment.rb:5:in `' from /home/zzak/code/rails/railties/lib/rails/application.rb:440:in `initialize!' from /home/zzak/code/rails/railties/lib/rails/initializable.rb:60:in `run_initializers' from /home/zzak/.rbenv/versions/3.3.5/lib/ruby/3.3.0/tsort.rb:208:in `tsort_each' from /home/zzak/.rbenv/versions/3.3.5/lib/ruby/3.3.0/tsort.rb:229:in `tsort_each' from /home/zzak/.rbenv/versions/3.3.5/lib/ruby/3.3.0/tsort.rb:350:in `each_strongly_connected_component' from /home/zzak/.rbenv/versions/3.3.5/lib/ruby/3.3.0/tsort.rb:350:in `call' from /home/zzak/.rbenv/versions/3.3.5/lib/ruby/3.3.0/tsort.rb:350:in `each' from /home/zzak/.rbenv/versions/3.3.5/lib/ruby/3.3.0/tsort.rb:352:in `block in each_strongly_connected_component' from /home/zzak/.rbenv/versions/3.3.5/lib/ruby/3.3.0/tsort.rb:418:in `each_strongly_connected_component_from' from /home/zzak/.rbenv/versions/3.3.5/lib/ruby/3.3.0/tsort.rb:418:in `call' from /home/zzak/code/rails/railties/lib/rails/initializable.rb:50:in `tsort_each_child' from /home/zzak/code/rails/railties/lib/rails/initializable.rb:50:in `each' from /home/zzak/.rbenv/versions/3.3.5/lib/ruby/3.3.0/tsort.rb:424:in `block in each_strongly_connected_component_from' from /home/zzak/.rbenv/versions/3.3.5/lib/ruby/3.3.0/tsort.rb:434:in `each_strongly_connected_component_from' from /home/zzak/.rbenv/versions/3.3.5/lib/ruby/3.3.0/tsort.rb:425:in `block (2 levels) in each_strongly_connected_component_from' from /home/zzak/.rbenv/versions/3.3.5/lib/ruby/3.3.0/tsort.rb:353:in `block (2 levels) in each_strongly_connected_component' from /home/zzak/.rbenv/versions/3.3.5/lib/ruby/3.3.0/tsort.rb:231:in `block in tsort_each' from /home/zzak/code/rails/railties/lib/rails/initializable.rb:61:in `block in run_initializers' from /home/zzak/code/rails/railties/lib/rails/initializable.rb:32:in `run' from /home/zzak/code/rails/railties/lib/rails/initializable.rb:32:in `instance_exec' from /home/zzak/code/rails/railties/lib/rails/engine.rb:645:in `block in ' from /home/zzak/code/rails/railties/lib/rails/engine.rb:645:in `each' from /home/zzak/code/rails/railties/lib/rails/engine.rb:646:in `block (2 levels) in ' from /home/zzak/code/rails/railties/lib/rails/engine.rb:692:in `load_config_initializer' from /home/zzak/code/rails/activesupport/lib/active_support/notifications.rb:212:in `instrument' from /home/zzak/code/rails/railties/lib/rails/engine.rb:693:in `block in load_config_initializer' from /home/zzak/code/rails/railties/lib/rails/engine.rb:693:in `load' from /tmp/d20241101-2349270-pkyv1o/dummy/config/initializers/activejob.rb:2:in `
' from /home/zzak/code/rails/activejob/test/support/integration/adapters/resque.rb:5:in `setup' from /home/zzak/code/rails/activejob/lib/active_job/queue_adapter.rb:52:in `queue_adapter=' from /home/zzak/code/rails/activejob/lib/active_job/queue_adapters.rb:136:in `lookup' from /home/zzak/code/rails/activejob/lib/active_job/queue_adapters.rb:136:in `const_get' from /home/zzak/.rbenv/versions/3.3.5/lib/ruby/gems/3.3.0/gems/zeitwerk-2.7.1/lib/zeitwerk/core_ext/kernel.rb:34:in `require' from /home/zzak/.rbenv/versions/3.3.5/lib/ruby/gems/3.3.0/gems/bootsnap-1.18.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require' from /home/zzak/.rbenv/versions/3.3.5/lib/ruby/3.3.0/bundled_gems.rb:75:in `block (2 levels) in replace_require' from /home/zzak/.rbenv/versions/3.3.5/lib/ruby/3.3.0/bundled_gems.rb:75:in `require' from /home/zzak/code/rails/activejob/lib/active_job/queue_adapters/resque_adapter.rb:3:in `
' from /home/zzak/.rbenv/versions/3.3.5/lib/ruby/gems/3.3.0/gems/zeitwerk-2.7.1/lib/zeitwerk/core_ext/kernel.rb:34:in `require' from /home/zzak/.rbenv/versions/3.3.5/lib/ruby/gems/3.3.0/gems/bootsnap-1.18.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require' from /home/zzak/.rbenv/versions/3.3.5/lib/ruby/3.3.0/bundled_gems.rb:75:in `block (2 levels) in replace_require' from /home/zzak/.rbenv/versions/3.3.5/lib/ruby/3.3.0/bundled_gems.rb:75:in `require' from /home/zzak/code/resque/lib/resque.rb:27:in `
' from /home/zzak/.rbenv/versions/3.3.5/lib/ruby/gems/3.3.0/gems/zeitwerk-2.7.1/lib/zeitwerk/core_ext/kernel.rb:34:in `require' from /home/zzak/.rbenv/versions/3.3.5/lib/ruby/gems/3.3.0/gems/bootsnap-1.18.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require' from /home/zzak/.rbenv/versions/3.3.5/lib/ruby/3.3.0/bundled_gems.rb:75:in `block (2 levels) in replace_require' from /home/zzak/.rbenv/versions/3.3.5/lib/ruby/3.3.0/bundled_gems.rb:75:in `require' ``` Interestingly, I started by working on resque/resque-scheduler#795, but after this change that fix no longer seems necessary. Although maybe safe to include anyways, and that PR also fixed the CI. --- lib/resque.rb | 1 - lib/resque/railtie.rb | 7 +++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/resque.rb b/lib/resque.rb index 19269e55c..709edf760 100644 --- a/lib/resque.rb +++ b/lib/resque.rb @@ -24,7 +24,6 @@ require 'resque/vendor/utf8_util' require 'resque/railtie' if defined?(Rails::Railtie) -require 'active_job/queue_adapters/resque_adapter' if defined?(ActiveJob) module Resque include Helpers diff --git a/lib/resque/railtie.rb b/lib/resque/railtie.rb index ea71e892c..2c8b615e8 100644 --- a/lib/resque/railtie.rb +++ b/lib/resque/railtie.rb @@ -6,5 +6,12 @@ class Railtie < Rails::Railtie # redefine ths task to load the rails env task "resque:setup" => :environment end + + initializer "resque.active_job" do + ActiveSupport.on_load(:active_job) do + require "active_job/queue_adapters/resque_adapter" + ActiveJob::Base.queue_adapter = :resque + end + end end end