RAILS acts_as_solr

来源:互联网 发布:软件测试论坛 编辑:程序博客网 时间:2024/06/05 15:18
  was using acts_as_searchable for one of my project, which uses Hyperestraier in background. Yesterday I decided to use acts_as_solr which uses solr(based on Lucene Java search library). I did all written in its Manual/Readme, but when I issued
view plainprint?
  1.    
  2. rake solr:start  

to start the solr server, it threw a heart breaking “Bad file descriptor” error, although acts_as_solr was working fine on one of my colleague’s linux machine.

I started digging around this and found that there is an issue in rake task that starts the solr server. Actually the problem was this rake task uses ‘fork’ which is not available on windows, also it only handles ‘ECONNREFUSED’ exception which is actually “Connection Refused” error raised by ruby on linux. But in windown it throws ‘EBADF’ which is “Bad file descriptor” error raised by ruby on windows.

So below is the hack for that:

view plainprint?
  1. desc 'Starts Solr. on windows . Options accepted: RAILS_ENV=your_env, PORT=XX. Defaults to development if none.'  
  2. task :start_win do  
  3.   begin  
  4.     n = Net::HTTP.new('localhost', SOLR_PORT)  
  5.     n.request_head('/').value   
  6.   
  7.   rescue Net::HTTPServerException #responding  
  8.     puts "Port #{SOLR_PORT} in use" and return  
  9.   
  10.   rescue Errno::EBADF #not responding  
  11.     Dir.chdir(SOLR_PATH) do  
  12.         exec "java -Dsolr.data.dir=solr/data/#{ENV['RAILS_ENV']} -Djetty.port=#{SOLR_PORT} -jar start.jar"  
  13.       sleep(5)  
  14.       puts "#{ENV['RAILS_ENV']} Solr started sucessfuly on #{SOLR_PORT}, pid: #{pid}."  
  15.     end  
  16.   end  
  17. end  

Just add this to vendor/plugins/acts_as_solr/lib/taks/solr.rake, and start solr server on windows by issuing

view plainprint?
  1. rake solr:start_win