Guest User

Untitled

a guest
Jun 25th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. #
  2. # Credits: Lee Hambley <lee dot hambley at gmail dot com>
  3. # If you benefit from or improve this code, please drop me a line!
  4. #
  5.  
  6. require 'erb'
  7. require 'socket'
  8. require 'logger'
  9. require 'bundler/cli'
  10.  
  11. class SolrServer
  12.  
  13. attr_accessor :daemon
  14.  
  15. attr_accessor :hostname
  16. attr_accessor :log_level
  17. attr_accessor :log_file
  18. attr_accessor :pid_file
  19. attr_accessor :port
  20. attr_accessor :solr_data_dir
  21. attr_accessor :solr_home
  22. attr_accessor :solr_jar
  23.  
  24. class << self
  25. attr_accessor :config_file
  26. end
  27.  
  28. def initialize
  29. configure
  30. @logger = Logger.new($stdout)
  31. options = {
  32. :identifier => identifier,
  33. :before_start => lambda { ensure_solr_wrapper_ready },
  34. :start_command => startup_script_filename,
  35. :ping_command => lambda { TCPSocket.new(hostname, port) },
  36. :pid_file => pid_file,
  37. :log_file => log_file,
  38. :timeout => 20
  39. }
  40. @daemon = DaemonController.new(options)
  41. self
  42. end
  43.  
  44. def start
  45. if @daemon.running?
  46. @logger.fatal "#{identifier} Already Running"
  47. else
  48. @logger.info "Starting #{identifier}"
  49. @daemon.start
  50. @logger.info "Started #{identifier} successfully!"
  51. end
  52. end
  53.  
  54. def stop
  55. if @daemon.running?
  56. @logger.info "Stopping #{identifier}"
  57. @daemon.stop
  58. @logger.info "Stopped #{identifier} successfully"
  59. else
  60. @logger.fatal "#{identifier} isn't running"
  61. end
  62. end
  63.  
  64. def running?
  65. @daemon.running?
  66. end
  67.  
  68. private
  69.  
  70. def configure
  71. YAML.load(ERB.new(File.open(self.class.config_file).read).result)[RACK_ENV]['solr'].each do |option, value|
  72. self.send("#{option}=", value)
  73. end
  74. end
  75.  
  76. def startup_command
  77. command = ["java"]
  78. command << "-Djetty.port=#{port}" if port
  79. command << "-Dsolr.data.dir=#{solr_data_dir}" if solr_data_dir
  80. command << "-Dsolr.solr.home=#{solr_home}" if solr_home
  81. command << "-Djava.util.logging.config.file=#{log_file}" if log_file
  82. command << "-Djava.util.logging.config.file=#{logging_config_path}" if logging_config_path
  83. command << '-jar' << solr_jar
  84. command.join(' ')
  85. end
  86.  
  87. def ensure_solr_wrapper_ready
  88. script = "#!/bin/sh
  89. SOLR_HOME=\"<%= solr_home %>\"
  90. PID_FILE=\"<%= pid_file %>\"
  91. LOG_FILE=\"<%= log_file %>\"
  92. STARTUP_COMMAND=\"<%= startup_command %>\"
  93. cd $SOLR_HOME && $STARTUP_COMMAND &> $LOG_FILE &
  94. echo `ps aux | grep -v grep | grep \"$STARTUP_COMMAND\" | awk '{print $2}'` > $PID_FILE\n"
  95. Dir.mkdir(File.dirname(startup_script_filename)) rescue nil
  96. File.unlink(startup_script_filename) if File.exists?(startup_script_filename)
  97. File.open(startup_script_filename, 'w+') do |f|
  98. f.write(ERB.new(script).result(binding).gsub(/^\s{16}/, ''))
  99. end
  100. File.chmod(0500, startup_script_filename)
  101. end
  102.  
  103. def startup_script_filename
  104. PADRINO_ROOT + "/tmp/scripts/start-solr-#{RACK_ENV}.sh"
  105. end
  106.  
  107. def identifier
  108. "Solr Server (#{RACK_ENV} environment)"
  109. end
  110.  
  111. def logging_config_path
  112. return @logging_config_path if defined?(@logging_config_path)
  113. @logging_config_path =
  114. if log_file
  115. logging_config = Tempfile.new('logging.properties')
  116. logging_config.puts("handlers = java.util.logging.FileHandler")
  117. logging_config.puts("java.util.logging.FileHandler.level = #{log_level.to_s.upcase}")
  118. logging_config.puts("java.util.logging.FileHandler.pattern = #{log_file}")
  119. logging_config.puts("java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter")
  120. logging_config.flush
  121. logging_config.close
  122. logging_config.path
  123. end
  124. end
  125.  
  126. end
Add Comment
Please, Sign In to add comment