Class: Riddle::Controller

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration, path) ⇒ Controller

Returns a new instance of Controller.



7
8
9
10
11
12
13
14
# File 'lib/riddle/controller.rb', line 7

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.



5
6
7
# File 'lib/riddle/controller.rb', line 5

def bin_path
  @bin_path
end

#indexer_binary_nameObject

Returns the value of attribute indexer_binary_name.



5
6
7
# File 'lib/riddle/controller.rb', line 5

def indexer_binary_name
  @indexer_binary_name
end

#pathObject

Returns the value of attribute path.



5
6
7
# File 'lib/riddle/controller.rb', line 5

def path
  @path
end

#searchd_binary_nameObject

Returns the value of attribute searchd_binary_name.



5
6
7
# File 'lib/riddle/controller.rb', line 5

def searchd_binary_name
  @searchd_binary_name
end

Instance Method Details

#index(*indices) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/riddle/controller.rb', line 22

def index(*indices)
  options = indices.last.is_a?(Hash) ? indices.pop : {}
  indices << '--all' if indices.empty?

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

  Riddle::ExecuteCommand.call command, options[:verbose]
end

#pidObject



58
59
60
61
62
63
64
# File 'lib/riddle/controller.rb', line 58

def pid
  if File.exists?(configuration.searchd.pid_file)
    File.read(configuration.searchd.pid_file)[/\d+/]
  else
    nil
  end
end

#rotateObject



66
67
68
# File 'lib/riddle/controller.rb', line 66

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

#running?Boolean

Returns:

  • (Boolean)


70
71
72
73
74
# File 'lib/riddle/controller.rb', line 70

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

#sphinx_versionObject



16
17
18
19
20
# File 'lib/riddle/controller.rb', line 16

def sphinx_version
  `#{indexer} 2>&1`[/Sphinx (\d+\.\d+(\.\d+|(?:-dev|(\-id64)?\-beta)))/, 1]
rescue
  nil
end

#start(options = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/riddle/controller.rb', line 32

def start(options = {})
  return if running?
  check_for_configuration_file

  command = "#{searchd} --pidfile --config \"#{@path}\""
  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



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/riddle/controller.rb', line 45

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