Rails [已解决] ActiveJob + Sidekiq 报错,uninitialized constant 怎么解决?

leekelby · 2016年02月24日 · 最后由 bianjp 回复于 2016年05月20日 · 3678 次阅读

A NameError occurred in background at 2016-02-23 16:57:48 +0800 :

uninitialized constant DetectFamousAndMajorJob /home/deployer/apps/app_name/shared/bundle/ruby/2.1.0/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:261:in `const_get'


Backtrace:

/home/deployer/apps/app_name/shared/bundle/ruby/2.1.0/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:261:in `const_get'
/home/deployer/apps/app_name/shared/bundle/ruby/2.1.0/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:261:in `block in constantize'
/home/deployer/apps/app_name/shared/bundle/ruby/2.1.0/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:259:in `each'
/home/deployer/apps/app_name/shared/bundle/ruby/2.1.0/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:259:in `inject'
/home/deployer/apps/app_name/shared/bundle/ruby/2.1.0/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:259:in `constantize'
/home/deployer/apps/app_name/shared/bundle/ruby/2.1.0/gems/activesupport-4.2.5/lib/active_support/core_ext/string/inflections.rb:66:in `constantize'
/home/deployer/apps/app_name/shared/bundle/ruby/2.1.0/gems/activejob-4.2.5/lib/active_job/core.rb:28:in `deserialize'
/home/deployer/apps/app_name/shared/bundle/ruby/2.1.0/gems/activejob-4.2.5/lib/active_job/execution.rb:20:in `execute'
/home/deployer/apps/app_name/shared/bundle/ruby/2.1.0/gems/activejob-4.2.5/lib/active_job/queue_adapters/sidekiq_adapter.rb:42:in `perform'
/home/deployer/apps/app_name/shared/bundle/ruby/2.1.0/gems/sidekiq-4.0.2/lib/sidekiq/processor.rb:150:in `execute_job'
/home/deployer/apps/app_name/shared/bundle/ruby/2.1.0/gems/sidekiq-4.0.2/lib/sidekiq/processor.rb:132:in `block (2 levels) in process'
/home/deployer/apps/app_name/shared/bundle/ruby/2.1.0/gems/sidekiq-4.0.2/lib/sidekiq/middleware/chain.rb:127:in `block in invoke'
/home/deployer/apps/app_name/shared/bundle/ruby/2.1.0/gems/exception_notification-4.0.1/lib/exception_notification/sidekiq.rb:8:in `call'
/home/deployer/apps/app_name/shared/bundle/ruby/2.1.0/gems/sidekiq-4.0.2/lib/sidekiq/middleware/chain.rb:129:in `block in invoke'
/home/deployer/apps/app_name/shared/bundle/ruby/2.1.0/gems/newrelic_rpm-3.14.1.311/lib/new_relic/agent/instrumentation/sidekiq.rb:33:in `block in call'
/home/deployer/apps/app_name/shared/bundle/ruby/2.1.0/gems/newrelic_rpm-3.14.1.311/lib/new_relic/agent/instrumentation/controller_instrumentation.rb:362:in `perform_action_with_newrelic_trace'
/home/deployer/apps/app_name/shared/bundle/ruby/2.1.0/gems/newrelic_rpm-3.14.1.311/lib/new_relic/agent/instrumentation/sidekiq.rb:29:in `call'
/home/deployer/apps/app_name/shared/bundle/ruby/2.1.0/gems/sidekiq-4.0.2/lib/sidekiq/middleware/chain.rb:129:in `block in invoke'
/home/deployer/apps/app_name/shared/bundle/ruby/2.1.0/gems/sidekiq-4.0.2/lib/sidekiq/middleware/server/active_record.rb:6:in `call'
/home/deployer/apps/app_name/shared/bundle/ruby/2.1.0/gems/sidekiq-4.0.2/lib/sidekiq/middleware/chain.rb:129:in `block in invoke'
/home/deployer/apps/app_name/shared/bundle/ruby/2.1.0/gems/sidekiq-4.0.2/lib/sidekiq/middleware/server/retry_jobs.rb:74:in `call'
/home/deployer/apps/app_name/shared/bundle/ruby/2.1.0/gems/sidekiq-4.0.2/lib/sidekiq/middleware/chain.rb:129:in `block in invoke'
/home/deployer/apps/app_name/shared/bundle/ruby/2.1.0/gems/sidekiq-4.0.2/lib/sidekiq/middleware/server/logging.rb:11:in `block in call'
/home/deployer/apps/app_name/shared/bundle/ruby/2.1.0/gems/sidekiq-4.0.2/lib/sidekiq/logging.rb:30:in `with_context'
/home/deployer/apps/app_name/shared/bundle/ruby/2.1.0/gems/sidekiq-4.0.2/lib/sidekiq/middleware/server/logging.rb:7:in `call'
/home/deployer/apps/app_name/shared/bundle/ruby/2.1.0/gems/sidekiq-4.0.2/lib/sidekiq/middleware/chain.rb:129:in `block in invoke'
/home/deployer/apps/app_name/shared/bundle/ruby/2.1.0/gems/sidekiq-4.0.2/lib/sidekiq/middleware/chain.rb:132:in `call'
/home/deployer/apps/app_name/shared/bundle/ruby/2.1.0/gems/sidekiq-4.0.2/lib/sidekiq/middleware/chain.rb:132:in `invoke'
/home/deployer/apps/app_name/shared/bundle/ruby/2.1.0/gems/sidekiq-4.0.2/lib/sidekiq/processor.rb:127:in `block in process'
/home/deployer/apps/app_name/shared/bundle/ruby/2.1.0/gems/sidekiq-4.0.2/lib/sidekiq/processor.rb:166:in `stats'
/home/deployer/apps/app_name/shared/bundle/ruby/2.1.0/gems/sidekiq-4.0.2/lib/sidekiq/processor.rb:126:in `process'
/home/deployer/apps/app_name/shared/bundle/ruby/2.1.0/gems/sidekiq-4.0.2/lib/sidekiq/processor.rb:79:in `process_one'
/home/deployer/apps/app_name/shared/bundle/ruby/2.1.0/gems/sidekiq-4.0.2/lib/sidekiq/processor.rb:67:in `run'
/home/deployer/apps/app_name/shared/bundle/ruby/2.1.0/gems/sidekiq-4.0.2/lib/sidekiq/util.rb:16:in `watchdog'
/home/deployer/apps/app_name/shared/bundle/ruby/2.1.0/gems/sidekiq-4.0.2/lib/sidekiq/util.rb:24:in `block in safe_thread'

Data:

* data: {:sidekiq=>
  {"class"=>"ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper",
   "wrapped"=>"DetectFamousAndMajorJob",
   "queue"=>"default",
   "args"=>
    [{"job_class"=>"DetectFamousAndMajorJob",
      "job_id"=>"d4b06924-41a6-4b5a-bd78-9c1fb698769f",
      "queue_name"=>"default",
      "arguments"=>[6601],
      "locale"=>"zh-CN"}],
   "retry"=>true,
   "jid"=>"8043b8395a79fcb69739ed38",
   "created_at"=>1456217431.8794465,
   "enqueued_at"=>1456217868.0243142,
   "error_message"=>"uninitialized constant DetectFamousAndMajorJob",
   "error_class"=>"NameError",
   "failed_at"=>1456217439.976102,
   "retry_count"=>3,
   "retried_at"=>1456217663.7122743}}

其它:

任务是脚本生成的:

rails g DetectFamousAndMajorJob

配置

# config/environments/production.rb
  config.cache_classes = true

  config.eager_load = true
#   config/sidekiq.yml
---
:verbose: false
:pidfile: ./tmp/pids/sidekiq.pid
:concurrency: 5
:timeout: 30
:queues:
  - default

邮件里经常有类似报错,一般 retry 几次就过了,但看着太烦了。可能是什么原因引起的,怎么解决?

一些 redis/sidekiq 配置问题,已解决

DetectFamousAndMajorJob 这个类的文件名发来看看

这个看起来像文件名和类名不符合 ActiveSupport Autoload 规则,而导致首次调用 DetectFamousAndMajorJob 类触发 autoload 的时候,无法找到正确的文件来 require

如果是多个 server,检查一下另外一台机器是不是没有更新到最新的代码。

咋解决的啊 宽哥 T_T

遇到同样问题。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号