Class: Sunspot::Solr::Server

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

Overview

:nodoc:

Direct Known Subclasses

Rails::Server

Constant Summary

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_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)

Constructor Details

- (Server) initialize(*args)

A new instance of Server



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

def initialize(*args)
  ensure_java_installed
  super(*args)
end

Instance Attribute Details

- (Object) bind_address

Returns the value of attribute bind_address



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

def bind_address
  @bind_address
end

- (Object) log_file

Returns the value of attribute log_file



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

def log_file
  @log_file
end

- (Object) log_level



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

def log_level
  @log_level || 'WARNING'
end

- (Object) max_memory

Returns the value of attribute max_memory



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

def max_memory
  @max_memory
end

- (Object) min_memory

Returns the value of attribute min_memory



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

def min_memory
  @min_memory
end

- (Object) pid_dir



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

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

- (Object) pid_file



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

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

- (Object) port

Returns the value of attribute port



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

def port
  @port
end

- (Object) solr_data_dir

Returns the value of attribute solr_data_dir



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

def solr_data_dir
  File.expand_path(@solr_data_dir || Dir.tmpdir)
end

- (Object) solr_home

Returns the value of attribute solr_home



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

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

- (Object) solr_jar



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

def solr_jar
  @solr_jar || SOLR_START_JAR
end

Instance Method Details

- (Object) bootstrap

Bootstrap a new solr_home by creating all required directories.

Returns

Boolean

success



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

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

- (Object) create_solr_directories

Create new solr_home, config, log and pid directories

Returns

Boolean

success



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

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

- (Object) install_solr_home

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 'sunspot_solr/lib/sunspot/solr/server.rb', line 173

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

- (Object) pid_path



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

def pid_path
  File.join(pid_dir, pid_file)
end

- (Object) run

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

Returns

Boolean

success



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'sunspot_solr/lib/sunspot/solr/server.rb', line 91

def run
  bootstrap

  command = ['java']
  command << "-Xms#{min_memory}" if min_memory
  command << "-Xmx#{max_memory}" if max_memory
  command << "-Djetty.port=#{port}" if port
  command << "-Djetty.host=#{bind_address}" if bind_address
  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

- (Object) start

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

Returns

Boolean

success



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

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', 'a')
      STDERR.reopen(STDOUT)
      run
    end
    FileUtils.mkdir_p(pid_dir)
    File.open(pid_path, 'w') do |file|
      file << pid
    end
  end
end

- (Object) stop

Stop the sunspot-solr server.

Returns

Boolean

success



115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'sunspot_solr/lib/sunspot/solr/server.rb', line 115

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