Skip to content

Commit

Permalink
Fix circular require from Rails AJ adapter
Browse files Browse the repository at this point in the history
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  `<main>'
        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 <main>'
        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  `<top (required)>'
        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  `<top (required)>'
        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  `<top (required)>'
        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  `<top (required)>'
        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 <class:Engine>'
        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 <class:Engine>'
        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  `<main>'
        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  `<main>'
        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  `<main>'
        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.
  • Loading branch information
zzak committed Nov 1, 2024
1 parent 408ba5b commit 13e6517
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
1 change: 0 additions & 1 deletion lib/resque.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions lib/resque/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 13e6517

Please sign in to comment.