Ruby: Circular dependency detected when using streams

When I run this code, 9/10 of the time Rails raises the RuntimeError: Circular dependency detected when autoloading SearchEngine constant

def search
  ts_start = Time.now
  results = []
  threads = []
  @engines.each do |name|
    threads << Thread.new do
      engine = "#{name.capitalize}Engine".constantize.new @params, @location
      results << engine.search  
      Thread.exit if results.length == @engines.length || (Time.now - ts_start) > @timeout
    end
  end
  threads.each { |t| t.abort_on_exception = false; t.join }
  unless results.length == 0 then
    # do some stuff...
  end
end

      

However, when I create a new "engine" instance before the new thread:

engine = "#{name.capitalize}Engine".constantize.new @params, @location    
threads << Thread.new do
  results << engine.search  

      

then it works fine in 9/10 cases. But this is still not 100%.

When I don't use streams at all, it works fine every time.

Any suggestions for changing the code?

+3


source to share





All Articles