Class: Sunspot::Solr::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/sunspot/solr/server.rb

Overview

:nodoc:

Constant Summary collapse

ServerError =

Raised if #stop is called but the server is not running

Class.new(RuntimeError)
AlreadyRunningError =
Class.new(ServerError)
NotRunningError =
Class.new(ServerError)
JavaMissing =
Class.new(ServerError)
SOLR_EXECUTABLE =

Name of the sunspot executable (shell script)

File.expand_path(
  File.join(File.dirname(__FILE__), '..', '..', '..', 'solr', 'bin', 'solr')
)
LOG_LEVELS =
Set['SEVERE', 'WARNING', 'INFO', 'CONFIG', 'FINE', 'FINER', 'FINEST']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeServer

Returns a new instance of Server.



26
27
28
# File 'lib/sunspot/solr/server.rb', line 26

def initialize
  Sunspot::Solr::Java.ensure_install!
end

Instance Attribute Details

#bind_addressObject

Returns the value of attribute bind_address.



22
23
24
# File 'lib/sunspot/solr/server.rb', line 22

def bind_address
  @bind_address
end

#log_fileObject

Returns the value of attribute log_file.



22
23
24
# File 'lib/sunspot/solr/server.rb', line 22

def log_file
  @log_file
end

#memoryObject

Returns the value of attribute memory.



22
23
24
# File 'lib/sunspot/solr/server.rb', line 22

def memory
  @memory
end

#pid_dirObject



145
146
147
# File 'lib/sunspot/solr/server.rb', line 145

def pid_dir
  File.expand_path(@pid_dir || FileUtils.pwd)
end

#pid_fileObject



141
142
143
# File 'lib/sunspot/solr/server.rb', line 141

def pid_file
  @pid_file || 'sunspot-solr.pid'
end

#portObject

Returns the value of attribute port.



22
23
24
# File 'lib/sunspot/solr/server.rb', line 22

def port
  @port
end

#solr_executableObject



153
154
155
# File 'lib/sunspot/solr/server.rb', line 153

def solr_executable
  @solr_executable || SOLR_EXECUTABLE
end

#solr_homeObject



149
150
151
# File 'lib/sunspot/solr/server.rb', line 149

def solr_home
  File.expand_path(@solr_home || File.join(File.dirname(solr_executable), '..', 'solr'))
end

Instance Method Details

#bootstrapObject

Bootstrap a new solr_home by creating all required directories.

Returns

Boolean

success



38
39
40
41
42
43
44
# File 'lib/sunspot/solr/server.rb', line 38

def bootstrap
  unless @bootstrapped
    install_solr_home
    create_solr_directories
    @bootstrapped = true
  end
end

#create_solr_directoriesObject

Create new solr_home, config, log and pid directories

Returns

Boolean

success



190
191
192
193
194
# File 'lib/sunspot/solr/server.rb', line 190

def create_solr_directories
  [pid_dir].each do |path|
    FileUtils.mkdir_p(path) unless File.exist?(path)
  end
end

#exec_in_solr_executable_directory(command) ⇒ Object



161
162
163
# File 'lib/sunspot/solr/server.rb', line 161

def exec_in_solr_executable_directory(command)
  FileUtils.cd(solr_executable_directory) { system(*command) }
end

#install_solr_homeObject

Copy default solr configuration files from sunspot gem to the new solr_home/config directory

Returns

Boolean

success



173
174
175
176
177
178
179
180
181
# File 'lib/sunspot/solr/server.rb', line 173

def install_solr_home
  unless File.exist?(solr_home)
    Sunspot::Solr::Installer.execute(
      solr_home,
      :force => true,
      :verbose => true
    )
  end
end

#log_levelObject



133
134
135
# File 'lib/sunspot/solr/server.rb', line 133

def log_level
  @log_level || 'WARNING'
end

#log_level=(level) ⇒ Object



126
127
128
129
130
131
# File 'lib/sunspot/solr/server.rb', line 126

def log_level=(level)
  unless LOG_LEVELS.include?(level.to_s.upcase)
    raise(ArgumentError, "#{level} is not a valid log level: Use one of #{LOG_LEVELS.to_a.join(',')}")
  end
  @log_level = level.to_s.upcase
end

#pid_pathObject



137
138
139
# File 'lib/sunspot/solr/server.rb', line 137

def pid_path
  File.join(pid_dir, pid_file)
end

#runObject

Run the sunspot-solr server in the foreground. Boostrap solr_home first, if neccessary.

Returns

Boolean

success



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/sunspot/solr/server.rb', line 90

def run
  bootstrap

  command = %w[./solr start -f]
  command << "-m" << "#{memory}" if memory
  command << "-p" << "#{port}" if port
  command << "-h" << "#{bind_address}" if bind_address
  command << "-s" << "#{solr_home}" if solr_home
  command << "-Dlog4j.configuration=file:#{logging_config.path}" if logging_config

  exec_in_solr_executable_directory(command)
end

#solr_executable_directoryObject



157
158
159
# File 'lib/sunspot/solr/server.rb', line 157

def solr_executable_directory
  @solr_executable_directory ||= File.dirname(solr_executable)
end

#startObject

Start the sunspot-solr server. Bootstrap solr_home first, if neccessary.

Returns

Boolean

success



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
# File 'lib/sunspot/solr/server.rb', line 54

def start
  bootstrap

  if File.exist?(pid_path)
    existing_pid = IO.read(pid_path).to_i
    begin
      Process.kill(0, existing_pid)
      raise(AlreadyRunningError, "Server is already running with PID #{existing_pid}")
    rescue Errno::ESRCH
      STDERR.puts("Removing stale PID file at #{pid_path}")
      FileUtils.rm(pid_path)
    end
  end
  fork do
    pid = fork do
      Process.setsid
      STDIN.reopen('/dev/null')
      STDOUT.reopen('/dev/null')
      STDERR.reopen(STDOUT)
      run
    end
    FileUtils.mkdir_p(pid_dir)
    File.open(pid_path, 'w') do |file|
      file << pid
    end
  end
end

#stopObject

Stop the sunspot-solr server.

Returns

Boolean

success



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/sunspot/solr/server.rb', line 110

def stop
  if File.exist?(pid_path)
    pid = IO.read(pid_path).to_i
    begin
      Process.kill('TERM', pid)
      exec_in_solr_executable_directory(['./solr', 'stop', '-p', "#{port}"]) if port
    rescue Errno::ESRCH
      raise NotRunningError, "Process with PID #{pid} is no longer running"
    ensure
      FileUtils.rm(pid_path)
    end
  else
    raise NotRunningError, "No PID file at #{pid_path}"
  end
end