启动Sidekiq队列时的循环依赖

时间:2015-09-15 14:16:05

标签: ruby-on-rails ruby constants sidekiq

加载sidekiq队列时出现以下错误:

RuntimeError: Circular dependency detected while autoloading constant FileProcessor
/Users/johnmcauley/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:492:in `load_missing_constant'
/Users/johnmcauley/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:184:in `const_missing'
/Users/johnmcauley/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:526:in `load_missing_constant'
/Users/johnmcauley/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:184:in `const_missing'
/Users/johnmcauley/workspace/wripl-capture/lib/wripl_article.rb:23:in `build_from_crawled_page'
/Users/johnmcauley/workspace/wripl-capture/app/workers/article_worker.rb:7:in `perform'
/Users/johnmcauley/.rvm/gems/ruby-2.2.2/gems/sidekiq-3.4.1/lib/sidekiq/processor.rb:75:in `execute_job'
/Users/johnmcauley/.rvm/gems/ruby-2.2.2/gems/sidekiq-3.4.1/lib/sidekiq/processor.rb:52:in `block (2 levels) in process'
/Users/johnmcauley/.rvm/gems/ruby-2.2.2/gems/sidekiq-3.4.1/lib/sidekiq/middleware/chain.rb:127:in `block in invoke'
/Users/johnmcauley/.rvm/gems/ruby-2.2.2/gems/sidekiq-3.4.1/lib/sidekiq/middleware/server/active_record.rb:6:in `call'
/Users/johnmcauley/.rvm/gems/ruby-2.2.2/gems/sidekiq-3.4.1/lib/sidekiq/middleware/chain.rb:129:in `block in invoke'
/Users/johnmcauley/.rvm/gems/ruby-2.2.2/gems/sidekiq-3.4.1/lib/sidekiq/middleware/server/retry_jobs.rb:74:in `call'
/Users/johnmcauley/.rvm/gems/ruby-2.2.2/gems/sidekiq-3.4.1/lib/sidekiq/middleware/chain.rb:129:in `block in invoke'
/Users/johnmcauley/.rvm/gems/ruby-2.2.2/gems/sidekiq-failures-0.4.4/lib/sidekiq/failures/middleware.rb:9:in `call'
/Users/johnmcauley/.rvm/gems/ruby-2.2.2/gems/sidekiq-3.4.1/lib/sidekiq/middleware/chain.rb:129:in `block in invoke'
/Users/johnmcauley/.rvm/gems/ruby-2.2.2/gems/sidekiq-3.4.1/lib/sidekiq/middleware/server/logging.rb:15:in `block in call'
/Users/johnmcauley/.rvm/gems/ruby-2.2.2/gems/sidekiq-3.4.1/lib/sidekiq/logging.rb:30:in `with_context'
/Users/johnmcauley/.rvm/gems/ruby-2.2.2/gems/sidekiq-3.4.1/lib/sidekiq/middleware/server/logging.rb:11:in `call'
/Users/johnmcauley/.rvm/gems/ruby-2.2.2/gems/sidekiq-3.4.1/lib/sidekiq/middleware/chain.rb:129:in `block in invoke'
/Users/johnmcauley/.rvm/gems/ruby-2.2.2/gems/sidekiq-3.4.1/lib/sidekiq/middleware/chain.rb:132:in `call'
/Users/johnmcauley/.rvm/gems/ruby-2.2.2/gems/sidekiq-3.4.1/lib/sidekiq/middleware/chain.rb:132:in `invoke'
/Users/johnmcauley/.rvm/gems/ruby-2.2.2/gems/sidekiq-3.4.1/lib/sidekiq/processor.rb:51:in `block in process'
/Users/johnmcauley/.rvm/gems/ruby-2.2.2/gems/sidekiq-3.4.1/lib/sidekiq/processor.rb:98:in `stats'
/Users/johnmcauley/.rvm/gems/ruby-2.2.2/gems/sidekiq-3.4.1/lib/sidekiq/processor.rb:50:in `process'
/Users/johnmcauley/.rvm/gems/ruby-2.2.2/gems/celluloid-0.16.0/lib/celluloid/calls.rb:26:in `public_send'
/Users/johnmcauley/.rvm/gems/ruby-2.2.2/gems/celluloid-0.16.0/lib/celluloid/calls.rb:26:in `dispatch'
/Users/johnmcauley/.rvm/gems/ruby-2.2.2/gems/celluloid-0.16.0/lib/celluloid/calls.rb:122:in `dispatch'
/Users/johnmcauley/.rvm/gems/ruby-2.2.2/gems/celluloid-0.16.0/lib/celluloid/cell.rb:60:in `block in invoke'
/Users/johnmcauley/.rvm/gems/ruby-2.2.2/gems/celluloid-0.16.0/lib/celluloid/cell.rb:71:in `block in task'
/Users/johnmcauley/.rvm/gems/ruby-2.2.2/gems/celluloid-0.16.0/lib/celluloid/actor.rb:357:in `block in task'
/Users/johnmcauley/.rvm/gems/ruby-2.2.2/gems/celluloid-0.16.0/lib/celluloid/tasks.rb:57:in `block in initialize'
/Users/johnmcauley/.rvm/gems/ruby-2.2.2/gems/celluloid-0.16.0/lib/celluloid/tasks/task_fiber.rb:15:in `block in create'

FileProcessor是lib中的常量。

我使用:

加载lib和worker
    config.autoload_paths += %W(
        #{config.root}/app/workers
        #{config.root}/lib
    )

文件处理器类是:

class FileProcessor

def self.update_totals(source_id, update_type)

    total = Total.first
    if total == nil
       total = Total.new
    end

    source = Source.find(source_id)

    if update_type.to_s == "ARTICLE"
      source.number_of_articles = source.number_of_articles + 1
      total.number_of_articles = total.number_of_articles + 1
    elsif update_type.to_s == "PAGE"
      source.number_of_pages = source.number_of_pages + 1
      total.number_of_pages = total.number_of_pages + 1
    elsif update_type.to_s == "RSS" 
      source.number_of_rss_articles = source.number_of_rss_articles + 1
      total.number_of_rss_articles = total.number_of_rss_articles + 1
    end

    source.save
    total.save
end
end

我环顾四周但找不到解决这个问题的方法。

有什么建议吗?

1 个答案:

答案 0 :(得分:7)

好的,这是一个authoload / eagerload问题。我找到了一些关于GH处理这个问题的讨论。我仍然需要在生产中测试,但添加:

config.autoload_paths += %W(
    #{config.root}/app/workers
    #{config.root}/lib
)
config.eager_load_paths += %W(
    #{config.root}/app/workers
    #{config.root}/lib
)

到application.rb修复了问题。

感谢:

https://github.com/mperham/sidekiq/issues/2060 https://github.com/mperham/sidekiq/issues/1927 http://blog.arkency.com/2014/11/dont-forget-about-eager-load-when-extending-autoload/