File: controller.rb

package info (click to toggle)
ruby-riddle 2.3.1-2~deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 10,752 kB
  • sloc: sql: 25,022; php: 5,992; ruby: 4,757; sh: 59; makefile: 5
file content (112 lines) | stat: -rw-r--r-- 2,898 bytes parent folder | download
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# frozen_string_literal: true

module Riddle
  NoConfigurationFileError = Class.new StandardError

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

    attr_accessor :path, :bin_path, :searchd_binary_name, :indexer_binary_name

    def initialize(configuration, path)
      @configuration  = configuration
      @path           = path

      @bin_path            = ''
      @searchd_binary_name = 'searchd'
      @indexer_binary_name = 'indexer'
    end

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

    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

    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

    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

    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

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

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

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

    private

    attr_reader :configuration

    def indexer
      "#{bin_path}#{indexer_binary_name}"
    end

    def searchd
      "#{bin_path}#{searchd_binary_name}"
    end

    def check_for_configuration_file
      return if File.exist?(@path)

      raise Riddle::NoConfigurationFileError, "'#{@path}' does not exist"
    end
  end
end