Class: Riddle::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/riddle/controller.rb

Constant Summary collapse

DEFAULT_MERGE_OPTIONS =
{:filters => {}}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration, path) ⇒ Controller

Returns a new instance of Controller.



11
12
13
14
15
16
17
18
# 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

Instance Attribute Details

#bin_pathObject

Returns the value of attribute bin_path.



9
10
11
# File 'lib/riddle/controller.rb', line 9

def bin_path
  @bin_path
end

#indexer_binary_nameObject

Returns the value of attribute indexer_binary_name.



9
10
11
# File 'lib/riddle/controller.rb', line 9

def indexer_binary_name
  @indexer_binary_name
end

#pathObject

Returns the value of attribute path.



9
10
11
# File 'lib/riddle/controller.rb', line 9

def path
  @path
end

#searchd_binary_nameObject

Returns the value of attribute searchd_binary_name.



9
10
11
# File 'lib/riddle/controller.rb', line 9

def searchd_binary_name
  @searchd_binary_name
end

Instance Method Details

#index(*indices) ⇒ Object



26
27
28
29
30
31
32
33
34
# 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 = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# 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

#pidObject



76
77
78
79
80
81
82
# 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

#rotateObject



84
85
86
# File 'lib/riddle/controller.rb', line 84

def rotate
  pid && Process.kill(:HUP, pid.to_i)
end

#running?Boolean

Returns:

  • (Boolean)


88
89
90
91
92
# File 'lib/riddle/controller.rb', line 88

def running?
  !!pid && !!Process.kill(0, pid.to_i)
rescue
  false
end

#sphinx_versionObject



20
21
22
23
24
# 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 = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# 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 = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# 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