Comment démarrer automatiquement le démon sphinx lorsque mon application Rails se charge?
-
11-09-2019 - |
Question
Je suis conscient de la commande de démarrer manuellement le démon sphinx. J'utilise une tâche de Rake: "Rake Thinking_sphinx: Démarrer" Est-il possible de le démarrer chaque fois que mon application Rails se charge donc je n'ai pas à taper manuellement la commande à chaque fois?
La solution
Vous pouvez configurer Daemon_Controller pour ce faire: http://blog.phusion.nl/2008/08/25/Daemon_Controller-a-Library-for-Robust-Daemon-management/
Autres conseils
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
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
Put the command to launch it in your config/initializers/custom.rb
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!
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 :(
Although this post is quite old, I add my solution just for the sake of completeness ...
I start the sphinx daemon without using rake, by putting the following code to config/initializers/launch_sphinx.rb.
Kernel.system("/usr/local/sphinx/bin/searchd --pidfile --config [full-path-to-your-app]/config/#{RAILS_ENV}.sphinx.config")
Important:
Change the paths to searchd and your rails application to your needs.