Rails; Autoload issue after installing Sidekiq: ArgumentError (A copy of Api::V1 has been removed from the module tree but is still active!):

懵懂的女人 提交于 2020-07-22 05:51:48

问题


I have a rails (v5.1) project in development, in which I recently installed Sidekiq. I now have an autoload issue, getting the subject argument error.

I have done a fair bit of research which seem to boil down to this being a problem of "trying to access an automatically reloaded class (in app directory) from one that is not automatically reloaded (in lib directory)," and that when the code is reloaded, Rails throws an error since the module from which the constant search is starting shouldn't be there but is. In my case Rails appears to be trying to autoload controllers under "api/v1." Key reference here.

Apparently, the problem can also be related to threading when a constant is loaded in another thread when autoload is trying to load the same constant in a different thread. I've read that the issue could be dealt with by "require" or "require_dependency" to make sure the code is loaded before it is attempted to be autoloaded by Rails, but I have not figured out where I can require controllers before autoload. Any insights?

I cannot seem to find issues with file naming conventions, which seems to be one reason for this error. I've tried putting "::" in different places, but to no avail. I can set "config.eager_load: true" in development.rb to run the code error free but that leaves open other issues.

********* UPDATE *********

I tried changing the thread pool size in /config/puma.rb from 5 to 1, and the ArgumentError disappeared. This suggests a threading issue described above and in this post, and not with a autoloaded constant in a non-autoloaded one. Does anyone know how to deal with this?

config/puma.rb

threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }

to

threads_count = ENV.fetch("RAILS_MAX_THREADS") { 1 }

********* UPDATE *********

Could the contents in my lib directory be the problem? If so, how can I get around this?

My app has only one file in lib directory for the responders gem, application_responder.rb, which references ActionController. Could this reference to be a problem? File contents are below:

/lib/application_responder.rb

class ApplicationResponder < ActionController::Responder
  include Responders::FlashResponder
  include Responders::HttpCacheResponder
end

The application_controller.rb file refers to responders as below:

/app/controllers/application_controller.rb

require "application_responder"

class ApplicationController < ActionController::API
  self.responder = ApplicationResponder
  respond_to :html
    
  include ActionController::MimeResponds
  include Response
  ...
end

Error logs:

ArgumentError (A copy of Api::V1 has been removed from the module tree but is still active!):
  
ArgumentError (A copy of Api::V1 has been removed from the module tree but is still active!):
/usr/local/Cellar/rbenv/1.1.2/versions/2.7.1/lib/ruby/gems/2.7.0/gems/activesupport-5.1.5/lib/active_support/dependencies.rb:457: warning: already initialized constant Api
/usr/local/Cellar/rbenv/1.1.2/versions/2.7.1/lib/ruby/gems/2.7.0/gems/activesupport-5.1.5/lib/active_support/dependencies.rb:457: warning: previous definition of Api was here
  
  
activesupport (5.1.5) lib/active_support/dependencies.rb:496:in `load_missing_constant'
activesupport (5.1.5) lib/active_support/dependencies.rb:202:in `const_missing'
activesupport (5.1.5) lib/active_support/inflector/methods.rb:271:in `const_get'
activesupport (5.1.5) lib/active_support/inflector/methods.rb:271:in `block in constantize'
activesupport (5.1.5) lib/active_support/inflector/methods.rb:267:in `each'
activesupport (5.1.5) lib/active_support/inflector/methods.rb:267:in `inject'
activesupport (5.1.5) lib/active_support/inflector/methods.rb:267:in `constantize'
activesupport (5.1.5) lib/active_support/dependencies.rb:583:in `get'
activesupport (5.1.5) lib/active_support/dependencies.rb:614:in `constantize'
actionpack (5.1.5) lib/action_dispatch/http/request.rb:85:in `controller_class_for'
actionpack (5.1.5) lib/action_dispatch/http/parameters.rb:99:in `binary_params_for?'
actionpack (5.1.5) lib/action_dispatch/http/parameters.rb:90:in `set_binary_encoding'
actionpack (5.1.5) lib/action_dispatch/http/parameters.rb:67:in `path_parameters='
actionpack (5.1.5) lib/action_dispatch/journey/router.rb:48:in `block in serve'
actionpack (5.1.5) lib/action_dispatch/journey/router.rb:33:in `each'
actionpack (5.1.5) lib/action_dispatch/journey/router.rb:33:in `serve'
actionpack (5.1.5) lib/action_dispatch/routing/route_set.rb:844:in `call'
bullet (5.6.1) lib/bullet/rack.rb:12:in `call'
warden (1.2.7) lib/warden/manager.rb:36:in `block in call'
warden (1.2.7) lib/warden/manager.rb:35:in `catch'
warden (1.2.7) lib/warden/manager.rb:35:in `call'
rack (2.0.4) lib/rack/etag.rb:25:in `call'
rack (2.0.4) lib/rack/conditional_get.rb:38:in `call'
rack (2.0.4) lib/rack/head.rb:12:in `call'
activerecord (5.1.5) lib/active_record/migration.rb:556:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/callbacks.rb:26:in `block in call'
activesupport (5.1.5) lib/active_support/callbacks.rb:97:in `run_callbacks'
actionpack (5.1.5) lib/action_dispatch/middleware/callbacks.rb:24:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/debug_exceptions.rb:59:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.1.5) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.1.5) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.1.5) lib/active_support/tagged_logging.rb:69:in `block in tagged'
activesupport (5.1.5) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.1.5) lib/active_support/tagged_logging.rb:69:in `tagged'
railties (5.1.5) lib/rails/rack/logger.rb:24:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/request_id.rb:25:in `call'
rack (2.0.4) lib/rack/runtime.rb:22:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/static.rb:125:in `call'
rack (2.0.4) lib/rack/sendfile.rb:111:in `call'
rack-cors (1.0.2) lib/rack/cors.rb:97:in `call'
railties (5.1.5) lib/rails/engine.rb:522:in `call'
puma (3.11.3) lib/puma/configuration.rb:225:in `call'
puma (3.11.3) lib/puma/server.rb:624:in `handle_request'
puma (3.11.3) lib/puma/server.rb:438:in `process_client'
puma (3.11.3) lib/puma/server.rb:302:in `block in run'
puma (3.11.3) lib/puma/thread_pool.rb:120:in `block in spawn_thread'
/usr/local/Cellar/rbenv/1.1.2/versions/2.7.1/lib/ruby/gems/2.7.0/gems/json-2.1.0/lib/json/common.rb:156: warning: Using the last argument as keyword parameters is deprecated
  

来源:https://stackoverflow.com/questions/62927007/rails-autoload-issue-after-installing-sidekiq-argumenterror-a-copy-of-apiv1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!