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


5
6
7
8
9
10
11
12
# 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

Instance Attribute Details

#bin_pathObject

Returns the value of attribute bin_path


3
4
5
# File 'lib/riddle/controller.rb', line 3

def bin_path
  @bin_path
end

#indexer_binary_nameObject

Returns the value of attribute indexer_binary_name


3
4
5
# File 'lib/riddle/controller.rb', line 3

def indexer_binary_name
  @indexer_binary_name
end

#pathObject

Returns the value of attribute path


3
4
5
# File 'lib/riddle/controller.rb', line 3

def path
  @path
end

#searchd_binary_nameObject

Returns the value of attribute searchd_binary_name


3
4
5
# File 'lib/riddle/controller.rb', line 3

def searchd_binary_name
  @searchd_binary_name
end

Instance Method Details

#index(*indices) ⇒ Object


20
21
22
23
24
25
26
27
# 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) : `#{cmd}`
end

#pidObject


70
71
72
73
74
75
76
# 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

#rotateObject


78
79
80
# File 'lib/riddle/controller.rb', line 78

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

#running?Boolean

Returns:

  • (Boolean)

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

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

#sphinx_versionObject


14
15
16
17
18
# File 'lib/riddle/controller.rb', line 14

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

#start(options = {}) ⇒ Object


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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 = `#{cmd}`
  end

  sleep(1)

  unless running?
    puts "Failed to start searchd daemon. Check #{@configuration.searchd.log}."
  end
  
  output
end

#stopObject


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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 = %(#{searchd} --pidfile --config "#{@path}" --#{stop_flag})

  if RUBY_PLATFORM =~ /mswin|mingw/
    system("start /B #{cmd} 1> NUL 2>&1")
  else
    `#{cmd}`
  end
ensure
  return !running?
end