我有一个正在开发的 rails (v5.1) 项目,我最近在其中安装了 Sidekiq。我现在有一个自动加载问题,得到主题参数错误。
我做了相当多的研究,这似乎归结为“试图从一个没有自动重新加载(在 lib 目录中)的类中访问自动重新加载的类(在 app 目录中)”的问题,并且当重新加载代码时,Rails 会抛出一个错误,因为常量搜索开始的模块不应该存在,而是存在。在我的例子中,Rails 似乎试图在“api/v1”下自动加载控制器。此处主要参考。
显然,当自动加载试图在不同的线程中加载相同的常量时,当在另一个线程中加载常量时,问题也可能与线程有关。我已经读过这个问题可以通过“require”或“require_dependency”来处理,以确保在Rails尝试自动加载代码之前加载代码,但我还没有弄清楚在自动加载之前我可以在哪里需要控制器。有什么见解吗?
我似乎找不到文件命名约定的问题,这似乎是导致此错误的原因之一。我试过把“::”放在不同的地方,但无济于事。我可以在 development.rb 中设置“config.eager_load:true”以无错误地运行代码,但这会留下其他问题。
********* 更新 *********
我尝试将 /config/puma.rb 中的线程池大小从 5 更改为 1,然后 ArgumentError 消失了。这表明上述和本文中描述的线程问题,而不是非自动加载的常量中的自动加载常量。我几乎得出结论,Sidekiq 由于线程而与 Rails 自动加载不兼容,但有谁知道如何处理这个问题?
配置/puma.rb
threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
至
threads_count = ENV.fetch("RAILS_MAX_THREADS") { 1 }
********* 更新 *********
我的 lib 目录中的内容可能是问题所在吗?如果是这样,我该如何解决这个问题?
我的应用程序在 lib 目录中只有一个文件用于响应程序 gem,application_responder.rb,它引用了 ActionController。这个参考可能有问题吗?文件内容如下:
/lib/application_responder.rb
class ApplicationResponder < ActionController::Responder
include Responders::FlashResponder
include Responders::HttpCacheResponder
end
application_controller.rb 文件引用响应者如下:
/app/controllers/application_controller.rb
require "application_responder"
class ApplicationController < ActionController::API
self.responder = ApplicationResponder
respond_to :html
include ActionController::MimeResponds
include Response
...
end
错误日志:
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