Acts_as_solr: Starting solr server on windows

Published on Author Akhil Bansal25 Comments

I 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

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:

  desc 'Starts Solr. on windows . Options accepted: RAILS_ENV=your_env, PORT=XX. Defaults to development if none.'
  task :start_win do
    begin
      n = Net::HTTP.new('localhost', SOLR_PORT)
      n.request_head('/').value

    rescue Net::HTTPServerException #responding
      puts "Port #{SOLR_PORT} in use" and return

    rescue Errno::EBADF #not responding
      Dir.chdir(SOLR_PATH) do
          exec "java -Dsolr.data.dir=solr/data/#{ENV['RAILS_ENV']} -Djetty.port=#{SOLR_PORT} -jar start.jar"
        sleep(5)
        puts "#{ENV['RAILS_ENV']} Solr started sucessfuly on #{SOLR_PORT}, pid: #{pid}."
      end
    end
  end

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

rake solr:start_win

25 Responses to Acts_as_solr: Starting solr server on windows

  1. This hack didn’t work for me until I renamed the task from :start_win to :startwin. The ‘_’ seems to be a problem in my rails app.

    @takemoto: Stop Solr by calling “rake solr:stop”

  2. I tried to work on windows. but whenever I try to run the rake start_win I got the following error “No connection could be made because the target machine actively refused it. – connect(2)”.
    My firewall is turned off, so I wonder what may be the problem?

  3. Ahmed,

    I had to change Errno::EBADF back to Errno::ECONNREFUSED to make it work on windows. The task tries to send an http request to the server, which isn’t running when you invoke the task, so it catches the excepiton and starts the server. For some reason, the exception being thrown is ECONNREFUSED, not EBADF.

  4. I’m using Aptana Rad Rails IDE(in Windows) for my rails development and I have not
    been able to get acts_as_solr installed.
    I tried doing
    1. script/plugin install svn://svn.railsfreaks.com/projects/acts_as_solr and
    2. script/plugin install git://github.com/railsfreaks/acts_as_solr.git

    with (1) nothing happens and with (2) I get “Plugin not found: [“git://github.com/railsfreaks/acts_as_solr.git”]” msg.

    can u plz help me on how to install acts_as_solr.. thanks a lott in advance

  5. Also.. For ppl finding it difficult to install the plugin in windows even after following the tutorial add this line right after the ‘task’ block.

    require “#{File.dirname(__FILE__)}/../../config/solr_environment.rb”

    also add the entire block of code in the end just before: “def env_array_to_constants(env)”

    Cheers!

Leave a Reply

Your email address will not be published. Required fields are marked *