views:

1044

answers:

6

I am aware of the command to start the sphinx daemon manually. I use a rake task: "rake thinking_sphinx:start" Is it possible to have it start whenever my rails application loads so I don't have to manually type in the command every time?

+2  A: 

You should be able to test if it's running and launch it from within rails (using back-ticks or the %x{...} notation.

Given that (as you said in the comments) it's a rake task you may want to do it like so instead of with back-ticks:

Rake::Task['thinking_sphinx:start'].invoke
MarkusQ
I tried putting `rake thinking_sphinx:start` in initializers/custom.rb with no luck. rails is trying to do something, but it freezes and the server never starts. An example would be very helpful
Tony
Odd. I've not done this with rake tasks. You may want to assure that you're in the right directory. Given that it's a rake task, I'll update my answer...
MarkusQ
thanks,better than before...but Rake is not being recognized. `load_missing_constant': uninitialized constant Rake (NameError)where would you put that code? i guess i need to include something or move the code somewhere else...
Tony
hmmm...so i added require 'rake' ...and now i get this error:Don't know how to build task 'thinking_sphinx:start' (RuntimeError)
Tony
Where is the task defined? You man need to put it in your application's lib/tasks/ or some such.
MarkusQ
+1  A: 

Put the command to launch it in your config/initializers/custom.rb

insane.dreamer
A: 

I've had to do the same thing in my app, but with windows. In case you're in the same sticky mess, you'll find that your life will be much easier if you do something like:

if app_not_already_running
  IO.popen("start app") do |fd|
  end
end

I'm looking at old code and I don't remember if the do |fd| was really necessary. Give it a shot.

The reason the 'start' is important is to con windows into backgrounding the cursed thing. Yargh!

i am basically just trying to run a rake task upon initialization, so not sure that applies
Tony
+1  A: 

If you are deploying via capistrano (and you should be), simply add it as an after_deploy:

desc "Run this after every successful deployment"

  task :after_deploy, :roles => :app do
        run "#{current_path}/rake thinking_sphinx:start"
  end
Matt Rogish
cool....i want this to happen on my local machine as well. i am thinking any time i start the server (ruby script/server), sphinx should start as well
Tony
+2  A: 

You can configure daemon_controller to do this: http://blog.phusion.nl/2008/08/25/daemon_controller-a-library-for-robust-daemon-management/

Anton Mironov
A: 

As mentioned above, create a file in config/initializers. For example, I created a file called initializers/start_thinking_sphinx.rb. And in the file I put

require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
require 'tasks/rails'
require "#{RAILS_ROOT}/vendor/plugins/thinking-sphinx/tasks/thinking_sphinx_tasks"
Rake::Task['thinking_sphinx:start'].invoke

This works if I then start the server with script/server. However does not work if I start with passenger :(