class Riddle::Controller

Constants

DEFAULT_MERGE_OPTIONS

Attributes

bin_path[RW]
configuration[R]
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 11
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 26
def index(*indices)
  options = indices.last.is_a?(Hash) ? indices.pop : {}
  indices << '--all' if indices.empty?

  command = "#{indexer} --config \"#{@path}\" #{indices.join(' ')}"
  command = "#{command} --rotate" if running?

  Riddle::ExecuteCommand.call command, options[:verbose]
end
merge(destination, source, options = {}) click to toggle source
# File lib/riddle/controller.rb, line 36
def merge(destination, source, options = {})
  options = DEFAULT_MERGE_OPTIONS.merge options

  command = "#{indexer} --config \"#{@path}\"".dup
  command << " --merge #{destination} #{source}"
  options[:filters].each do |attribute, value|
    value = value..value unless value.is_a?(Range)
    command << " --merge-dst-range #{attribute} #{value.min} #{value.max}"
  end
  command << " --rotate" if running?

  Riddle::ExecuteCommand.call command, options[:verbose]
end
pid() click to toggle source
# File lib/riddle/controller.rb, line 76
def pid
  if File.exist?(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 84
def rotate
  pid && Process.kill(:HUP, pid.to_i)
end
running?() click to toggle source
# File lib/riddle/controller.rb, line 88
def running?
  !!pid && !!Process.kill(0, pid.to_i)
rescue
  false
end
sphinx_version() click to toggle source
# File lib/riddle/controller.rb, line 20
def sphinx_version
  `#{indexer} 2>&1`[/(Sphinx|Manticore) (\d+\.\d+(\.\d+|(?:-dev|(\-id64)?\-beta)))/, 2]
rescue
  nil
end
start(options = {}) click to toggle source
# File lib/riddle/controller.rb, line 50
def start(options = {})
  return if running?
  check_for_configuration_file

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

  exec(command) if options[:nodetach]

  # Code does not get here if nodetach is true.
  Riddle::ExecuteCommand.call command, options[:verbose]
end
stop(options = {}) click to toggle source
# File lib/riddle/controller.rb, line 63
def stop(options = {})
  return true unless running?
  check_for_configuration_file

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

  result = Riddle::ExecuteCommand.call command, options[:verbose]
  result.successful = !running?
  result
end

Private Instance Methods

check_for_configuration_file() click to toggle source
# File lib/riddle/controller.rb, line 106
def check_for_configuration_file
  return if File.exist?(@path)

  raise Riddle::NoConfigurationFileError, "'#{@path}' does not exist"
end
indexer() click to toggle source
# File lib/riddle/controller.rb, line 98
def indexer
  "#{bin_path}#{indexer_binary_name}"
end
searchd() click to toggle source
# File lib/riddle/controller.rb, line 102
def searchd
  "#{bin_path}#{searchd_binary_name}"
end