sphinx.rake 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. namespace :sphinx do
  2. desc 'Run indexer for configured indexes'
  3. task :index do
  4. config = load_config
  5. if config[:indexes]
  6. system "#{config[:root_dir]}/indexer --config \"#{config[:config_file]}\" #{config[:indexes]}"
  7. else
  8. puts 'You should specify indexes in sphinx.yml'
  9. end
  10. end
  11. desc 'Run indexer for all indexes'
  12. task :index_all do
  13. config = load_config
  14. system "#{config[:root_dir]}/indexer --config \"#{config[:config_file]}\" --all"
  15. end
  16. desc 'Rotate configured indexes and restart searchd server'
  17. task :rotate do
  18. config = load_config
  19. if config[:indexes]
  20. system "#{config[:root_dir]}/indexer --config \"#{config[:config_file]}\" --rotate #{config[:indexes]}"
  21. else
  22. puts 'You should specify indexes in sphinx.yml'
  23. end
  24. end
  25. desc 'Rotate all indexes and restart searchd server'
  26. task :rotate_all do
  27. config = load_config
  28. system "#{config[:root_dir]}/indexer --config \"#{config[:config_file]}\" --rotate --all"
  29. end
  30. desc 'Start searchd server'
  31. task :start do
  32. config = load_config
  33. if File.exists?(config[:pid_file])
  34. puts 'Sphinx searchd server is already started.'
  35. else
  36. system "#{config[:root_dir]}/searchd --config \"#{config[:config_file]}\""
  37. puts 'Sphinx searchd server started.'
  38. end
  39. end
  40. desc 'Stop searchd server'
  41. task :stop do
  42. config = load_config
  43. unless File.exists?(config[:pid_file])
  44. puts 'Sphinx searchd server is not running.'
  45. else
  46. pid = File.read(config[:pid_file]).chomp
  47. kill 'SIGHUP', pid
  48. puts 'Sphinx searchd server stopped.'
  49. end
  50. end
  51. desc 'Restart searchd server'
  52. task :restart => [:stop, :start]
  53. def load_config
  54. return @sphinx_config if @sphinx_config
  55. options = YAML.load_file(File.dirname(__FILE__) + '/../../../../config/sphinx.yml') rescue {}
  56. @sphinx_config = {
  57. :config_file => options['config_file'] || '/etc/sphinx.conf',
  58. :root_dir => options['root_dir'] || '/usr/bin',
  59. :indexes => options['indexes']
  60. }
  61. sphinx_config = File.read(@sphinx_config[:config_file]) rescue ''
  62. sphinx_config =~ /searchd\s*{.*pid_file\s*=\s*(.*?)\n.*}/m
  63. @sphinx_config[:pid_file] = $1 || '/var/run/searchd.pid'
  64. return @sphinx_config
  65. end
  66. end