class Riddle::Controller

Attributes

bin_path[RW]
indexer_binary_name[RW]
path[RW]
searchd_binary_name[RW]

Public Class Methods

new(configuration, path) click to toggle source
# File lib/riddle/controller.rb, line 5
def initialize(configuration, path)
  @configuration  = configuration
  @path           = path

  @bin_path            = ''
  @searchd_binary_name = 'searchd'
  @indexer_binary_name = 'indexer'
end

Public Instance Methods

index(*indices) click to toggle source
# File lib/riddle/controller.rb, line 20
def index(*indices)
  options = indices.last.is_a?(Hash) ? indices.pop : {}
  indices << '--all' if indices.empty?

  cmd = "#{indexer} --config \"#{@path}\" #{indices.join(' ')}"
  cmd << " --rotate" if running?
  options[:verbose] ? system(cmd) : %x`#{cmd}`
end
pid() click to toggle source
# File lib/riddle/controller.rb, line 70
def pid
  if File.exists?(@configuration.searchd.pid_file)
    File.read(@configuration.searchd.pid_file)[/\d+/]
  else
    nil
  end
end
rotate() click to toggle source
# File lib/riddle/controller.rb, line 78
def rotate
  pid && Process.kill(:HUP, pid.to_i)
end
running?() click to toggle source
# File lib/riddle/controller.rb, line 82
def running?
  !!pid && !!Process.kill(0, pid.to_i)
rescue
  false
end
sphinx_version() click to toggle source
# File lib/riddle/controller.rb, line 14
def sphinx_version
  %x`#{indexer} 2>&1`[/Sphinx (\d+\.\d+(\.\d+|(?:-dev|(\-id64)?\-beta)))/, 1]
rescue
  nil
end
start(options={}) click to toggle source
# File lib/riddle/controller.rb, line 29
def start(options={})
  return if running?
  check_for_configuration_file

  cmd = "#{searchd} --pidfile --config \"#{@path}\""
  cmd << " --nodetach" if options[:nodetach]

  if options[:nodetach]
    exec(cmd)
  elsif RUBY_PLATFORM =~ /mswin|mingw/
    output = system("start /B #{cmd} 1> NUL 2>&1")
  else
    output = %x`#{cmd}`
  end

  sleep(1)

  unless running?
    puts "Failed to start searchd daemon. Check #{@configuration.searchd.log}."
  end
  
  output
end
stop() click to toggle source
# File lib/riddle/controller.rb, line 53
def stop
  return true unless running?
  check_for_configuration_file

  stop_flag = 'stopwait'
  stop_flag = 'stop' if Riddle.loaded_version.split('.').first == '0'
  cmd = %Q(#{searchd} --pidfile --config "#{@path}" --#{stop_flag})

  if RUBY_PLATFORM =~ /mswin|mingw/
    system("start /B #{cmd} 1> NUL 2>&1")
  else
    %x`#{cmd}`
  end

  !running?
end

Private Instance Methods

check_for_configuration_file() click to toggle source
# File lib/riddle/controller.rb, line 98
def check_for_configuration_file
  return if File.exist?(@path)
  raise "Configuration file '#{@path}' does not exist"
end
indexer() click to toggle source
# File lib/riddle/controller.rb, line 90
def indexer
  "#{bin_path}#{indexer_binary_name}"
end
searchd() click to toggle source
# File lib/riddle/controller.rb, line 94
def searchd
  "#{bin_path}#{searchd_binary_name}"
end