Report abuse


			
ENV['RAILS_ENV'] ||= "development"

namespace :s do
  desc "Run indexer"
  task :index => :environment do
    cmd = "indexer --config #{config_file} --all"
    cmd << " --rotate" if sphinx_daemon_running?
    system cmd
  end

  desc "Rotate idendexes and restart searchd server"
  task :rotate => :environment do
    system "indexer --config #{config_file} --rotate --all"
  end

  desc "Start searchd server"
  task :start => :environment do
    if sphinx_daemon_running?
      puts 'Sphinx searchd server is already started.'
    else
      system "searchd --config #{config_file}"
      puts 'Sphinx searchd server started.'
    end
  end

  desc "Stop searchd server"
  task :stop => :environment do
    unless sphinx_daemon_running?
      puts 'Sphinx searchd server is not running.'
    else
      pid = File.read('log/searchd.pid').chomp
      system "kill #{pid}"
      puts 'Sphinx searchd server stopped.'
    end
  end

  desc "Check if the search daemon is running"
  task :status => :environment do
    if sphinx_daemon_running?
      puts "Daemon is running."
    else
      puts "Daemon is stopped."
    end
  end

  desc "Restart searchd server"
  task :restart => [:stop, :start]
end

def config_file
  File.expand_path("config/sphinx.#{ENV['RAILS_ENV']}.conf", RAILS_ROOT)
end

def sphinx_daemon_running?
  File.exists?('log/searchd.pid')
end