• RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
  • Youtube

Acts_as_solr: Starting solr server on windows

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
[ruby]
rake solr:start
[/ruby]
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:

[ruby]
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
[/ruby]

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

[ruby]
rake solr:start_win
[/ruby]

25 Responses so far.

  1. Henrik N says:

    Would be great if you filed a ticket for this issue: http://acts_as_solr.railsfreaks.com/newticket

  2. bhaveh says:

    coool…bro !!!!

  3. Patrick says:

    Thank you!! This solved our problem in seconds. Really appreciate the work you did.

  4. takemoto says:

    Thank you! but how can we stop solr ?
    Ctrl^C is one way, but I wish to stop from script..

  5. ChrisG says:

    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”

  6. Amir S. says:

    Thanks buddy! it worked but I didn’t need Errno::EBADF (i left that part alone)

    cheers

  7. Ahmed says:

    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?

  8. alex says:

    Ahmed,

    Make sure MySQL service is running.

  9. Immad says:

    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.

  10. Leon Li says:

    Immad

    Thanks for your advice. It works for me.

  11. Akhil Bansal says:

    Thanks to all of you guys. I am pleased to know that it helped.


    Akhil

  12. ahmed says:

    @Immad

    Yes, it worked perfectly after I changed the errorno.

    Thx very much guys for your help

  13. sandip says:

    Good Work.

    Are you having any idea how to start with webrick while deployment.

    Thanks!

  14. Akhil Bansal says:

    @sandip: Could you be more specific what do you want to do?

  15. sandip says:

    @akil
    When i start webrick server,
    that time i want to start solr server also in background.

  16. Akhil Bansal says:

    @sandp: No, you can’t hook that with webrick. you need to write a script that start webrick and solr.

  17. sandip says:

    @akil
    k, Have you any idea how to write that script ?

  18. Akhil Bansal says:

    @sandip: Its up to you, you can write a ruby script or a bash script.

    in ruby script:

    system(“ruby script/server”)
    system(“rake solr:start_win”)

    some thing like that

  19. sandip says:

    @akil

    Can you clarify bit more ?
    Because, i have not written any script yet.

    & also i will prefer ruby script

    so,How will be that exactly ?

  20. sandip says:

    finally, I found how to start solr in deamon.

  21. reddy says:

    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

  22. evindha says:

    I had the problem like Ahmed, until I change the solr.rake file becomes like this : http://pastie.org/632975

  23. Shripad says:

    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!


Asset Pipeline has been extracted a...

The asset pipeline which was introduced in Rails 3.1, has ...

ClientSideValidations: Add/Map mode...

https://twitter.com/bansalakhil/status/256699237230845952

Action and Page caching has been ex...

With the commit c82cf81f00f Action and Page caching has been extracted ...

turbo-sprockets-rails3: Speeds up ...

http://twitter.com/bansalakhil/status/253420357841723393

EdgeRails: ActiveRecord::SessionSto...

With the commit 3324e28804 ActiveRecord::SessionStore is extracted out of Rails into ...

Asset Pipeline has been extracted a...

The asset pipeline which was introduced in Rails 3.1, has ...

ClientSideValidations: Add/Map mode...

https://twitter.com/bansalakhil/status/256699237230845952

Action and Page caching has been ex...

With the commit c82cf81f00f Action and Page caching has been extracted ...

turbo-sprockets-rails3: Speeds up ...

http://twitter.com/bansalakhil/status/253420357841723393

EdgeRails: ActiveRecord::SessionSto...

With the commit 3324e28804 ActiveRecord::SessionStore is extracted out of Rails into ...