forked from pat/thinking-sphinx
/
controller.rb
49 lines (40 loc) · 926 Bytes
/
controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
module Riddle
class Controller
def initialize(configuration, path)
@configuration = configuration
@path = path
end
def index
cmd = "indexer --config #{@path} --all"
cmd << " --rotate" if running?
`#{cmd}`
end
def start
return if running?
cmd = "searchd --pidfile --config #{@path}"
`#{cmd}`
sleep(1)
unless running?
puts "Failed to start searchd daemon. Check #{@configuration.searchd.log}."
end
end
def stop
return unless running?
Process.kill('SIGTERM', pid)
rescue Errno::EINVAL
Process.kill('SIGKILL', pid)
end
def pid
if File.exists?(@configuration.searchd.pid_file)
File.read(@configuration.searchd.pid_file)[/\d+/]
else
nil
end
end
def running?
!!pid && !!Process.kill(0, pid)
rescue
false
end
end
end