Class: Sunspot::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/sunspot/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)
SOLR_START_JAR =

Name of the sunspot executable (shell script)

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#log_fileObject

Returns the value of attribute log_file.



19
20
21
# File 'lib/sunspot/server.rb', line 19

def log_file
  @log_file
end

#log_levelObject



107
108
109
# File 'lib/sunspot/server.rb', line 107

def log_level
  @log_level || 'WARNING'
end

#max_memoryObject

Returns the value of attribute max_memory.



19
20
21
# File 'lib/sunspot/server.rb', line 19

def max_memory
  @max_memory
end

#min_memoryObject

Returns the value of attribute min_memory.



19
20
21
# File 'lib/sunspot/server.rb', line 19

def min_memory
  @min_memory
end

#pid_dirObject



119
120
121
# File 'lib/sunspot/server.rb', line 119

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

#pid_fileObject



115
116
117
# File 'lib/sunspot/server.rb', line 115

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

#portObject

Returns the value of attribute port.



19
20
21
# File 'lib/sunspot/server.rb', line 19

def port
  @port
end

#solr_data_dirObject

Returns the value of attribute solr_data_dir.



19
20
21
# File 'lib/sunspot/server.rb', line 19

def solr_data_dir
  @solr_data_dir
end

#solr_homeObject

Returns the value of attribute solr_home.



19
20
21
# File 'lib/sunspot/server.rb', line 19

def solr_home
  @solr_home
end

#solr_jarObject



131
132
133
# File 'lib/sunspot/server.rb', line 131

def solr_jar
  @solr_jar || SOLR_START_JAR
end

Instance Method Details

#pid_pathObject



111
112
113
# File 'lib/sunspot/server.rb', line 111

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



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/sunspot/server.rb', line 64

def run
  command = ['java']
  command << "-Xms#{min_memory}" if min_memory
  command << "-Xmx#{max_memory}" if max_memory
  command << "-Djetty.port=#{port}" if port
  command << "-Dsolr.data.dir=#{solr_data_dir}" if solr_data_dir
  command << "-Dsolr.solr.home=#{solr_home}" if solr_home
  command << "-Djava.util.logging.config.file=#{logging_config_path}" if logging_config_path
  command << '-jar' << File.basename(solr_jar)
  FileUtils.cd(File.dirname(solr_jar)) do
    exec(Escape.shell_command(command))
  end
end

#startObject

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

Returns

Boolean

success



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

def start
  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', 'a')
      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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/sunspot/server.rb', line 85

def stop
  if File.exist?(pid_path)
    pid = IO.read(pid_path).to_i
    begin
      Process.kill('TERM', pid)
    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