我目前遇到的问题是,在我的Rails应用中使用Sidekiq引发了循环依赖性错误。只有在使用perform_async
时才会引发此问题,否则会成功运行。
以下是问题的简化演示。
Sidekiq工人:
# app/workers/normalize_fsbo_worker.rb
class NormalizeFSBOWorker
include Sidekiq::Worker
def perform(property_id)
puts property_id
end
end
Raketask:
# lib/tasks/normalize.rake
require 'rake'
namespace :hh do
namespace :normalize do
desc "Async normalize all fsbo"
task :fsbo_properties => [:environment] do |t, args|
Property.where('fsbo IS NULL').find_in_batches do |group|
group.each do |property|
NormalizeFSBOWorker.perform_async(property.id) # Circular Dependency error
# NormalizeFSBOWorker.new.perform(property.id) # Works
end
end
end
end
end
控制台:
$ be rake hh:normalize:fsbo_properties
Sidekiq日志Stacktrace:
2014-05-21T16:19:48Z 4627 TID-osxsorcmw WARN: {"retry"=>true, "queue"=>"default", "class"=>"NormalizeFSBOWorker", "args"=>[3120], "jid"=>"c84212e827ba1185794cab62", "enqueued_at"=>1400689188.5927122}
2014-05-21T16:19:48Z 4627 TID-osxsorcmw WARN: Circular dependency detected while autoloading constant NormalizeFSBOWorker
2014-05-21T16:19:48Z 4627 TID-osxsorcmw WARN: /home/lucas/.gem/ruby/2.1.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:460:in `load_missing_constant'
/home/lucas/.gem/ruby/2.1.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:183:in `const_missing'
/home/lucas/.gem/ruby/2.1.0/gems/activesupport-4.0.0/lib/active_support/inflector/methods.rb:226:in `const_get'
/home/lucas/.gem/ruby/2.1.0/gems/activesupport-4.0.0/lib/active_support/inflector/methods.rb:226:in `block in constantize'
/home/lucas/.gem/ruby/2.1.0/gems/activesupport-4.0.0/lib/active_support/inflector/methods.rb:224:in `each'
/home/lucas/.gem/ruby/2.1.0/gems/activesupport-4.0.0/lib/active_support/inflector/methods.rb:224:in `inject'
/home/lucas/.gem/ruby/2.1.0/gems/activesupport-4.0.0/lib/active_support/inflector/methods.rb:224:in `constantize'
/home/lucas/.gem/ruby/2.1.0/gems/activesupport-4.0.0/lib/active_support/core_ext/string/inflections.rb:66:in `constantize'
/home/lucas/.gem/ruby/2.1.0/gems/sidekiq-3.0.2/lib/sidekiq/processor.rb:46:in `process'
/home/lucas/.gem/ruby/2.1.0/gems/celluloid-0.15.2/lib/celluloid/calls.rb:25:in `public_send'
/home/lucas/.gem/ruby/2.1.0/gems/celluloid-0.15.2/lib/celluloid/calls.rb:25:in `dispatch'
/home/lucas/.gem/ruby/2.1.0/gems/celluloid-0.15.2/lib/celluloid/calls.rb:122:in `dispatch'
/home/lucas/.gem/ruby/2.1.0/gems/celluloid-0.15.2/lib/celluloid/actor.rb:322:in `block in handle_message'
/home/lucas/.gem/ruby/2.1.0/gems/celluloid-0.15.2/lib/celluloid/actor.rb:416:in `block in task'
/home/lucas/.gem/ruby/2.1.0/gems/celluloid-0.15.2/lib/celluloid/tasks.rb:55:in `block in initialize'
/home/lucas/.gem/ruby/2.1.0/gems/celluloid-0.15.2/lib/celluloid/tasks/task_fiber.rb:13:in `block in create'
那是怎么回事?这个循环引用在哪里?为什么不用#new.perform
?