Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- # Credits: Lee Hambley <lee dot hambley at gmail dot com>
- # If you benefit from or improve this code, please drop me a line!
- #
- require 'erb'
- require 'socket'
- require 'logger'
- require 'bundler/cli'
- class SolrServer
- attr_accessor :daemon
- attr_accessor :hostname
- attr_accessor :log_level
- attr_accessor :log_file
- attr_accessor :pid_file
- attr_accessor :port
- attr_accessor :solr_data_dir
- attr_accessor :solr_home
- attr_accessor :solr_jar
- class << self
- attr_accessor :config_file
- end
- def initialize
- configure
- @logger = Logger.new($stdout)
- options = {
- :identifier => identifier,
- :before_start => lambda { ensure_solr_wrapper_ready },
- :start_command => startup_script_filename,
- :ping_command => lambda { TCPSocket.new(hostname, port) },
- :pid_file => pid_file,
- :log_file => log_file,
- :timeout => 20
- }
- @daemon = DaemonController.new(options)
- self
- end
- def start
- if @daemon.running?
- @logger.fatal "#{identifier} Already Running"
- else
- @logger.info "Starting #{identifier}"
- @daemon.start
- @logger.info "Started #{identifier} successfully!"
- end
- end
- def stop
- if @daemon.running?
- @logger.info "Stopping #{identifier}"
- @daemon.stop
- @logger.info "Stopped #{identifier} successfully"
- else
- @logger.fatal "#{identifier} isn't running"
- end
- end
- def running?
- @daemon.running?
- end
- private
- def configure
- YAML.load(ERB.new(File.open(self.class.config_file).read).result)[RACK_ENV]['solr'].each do |option, value|
- self.send("#{option}=", value)
- end
- end
- def startup_command
- command = ["java"]
- 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=#{log_file}" if log_file
- command << "-Djava.util.logging.config.file=#{logging_config_path}" if logging_config_path
- command << '-jar' << solr_jar
- command.join(' ')
- end
- def ensure_solr_wrapper_ready
- script = "#!/bin/sh
- SOLR_HOME=\"<%= solr_home %>\"
- PID_FILE=\"<%= pid_file %>\"
- LOG_FILE=\"<%= log_file %>\"
- STARTUP_COMMAND=\"<%= startup_command %>\"
- cd $SOLR_HOME && $STARTUP_COMMAND &> $LOG_FILE &
- echo `ps aux | grep -v grep | grep \"$STARTUP_COMMAND\" | awk '{print $2}'` > $PID_FILE\n"
- Dir.mkdir(File.dirname(startup_script_filename)) rescue nil
- File.unlink(startup_script_filename) if File.exists?(startup_script_filename)
- File.open(startup_script_filename, 'w+') do |f|
- f.write(ERB.new(script).result(binding).gsub(/^\s{16}/, ''))
- end
- File.chmod(0500, startup_script_filename)
- end
- def startup_script_filename
- PADRINO_ROOT + "/tmp/scripts/start-solr-#{RACK_ENV}.sh"
- end
- def identifier
- "Solr Server (#{RACK_ENV} environment)"
- end
- def logging_config_path
- return @logging_config_path if defined?(@logging_config_path)
- @logging_config_path =
- if log_file
- logging_config = Tempfile.new('logging.properties')
- logging_config.puts("handlers = java.util.logging.FileHandler")
- logging_config.puts("java.util.logging.FileHandler.level = #{log_level.to_s.upcase}")
- logging_config.puts("java.util.logging.FileHandler.pattern = #{log_file}")
- logging_config.puts("java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter")
- logging_config.flush
- logging_config.close
- logging_config.path
- end
- end
- end
Add Comment
Please, Sign In to add comment