diff --git a/exe/solr_wrapper b/exe/solr_wrapper index 3363757..9daf731 100755 --- a/exe/solr_wrapper +++ b/exe/solr_wrapper @@ -7,8 +7,11 @@ options = {} collection_options = {} subtext = < OptionParser.new do |opts| - opts.banner = "Usage: clean" - end, - 'dir' => OptionParser.new do |opts| - opts.banner = "Usage: dir" - end, + 'clean' => OptionParser.new do |opts| + opts.banner = "Usage: clean" + end, + 'purge' => OptionParser.new do |opts| + opts.banner = "Usage: purge" + end, + 'dir' => OptionParser.new do |opts| + opts.banner = "Usage: dir" + end, + 'status' => OptionParser.new do |opts| + opts.banner = "Usage: status" + end, + 'stop' => OptionParser.new do |opts| + opts.banner = "Usage: stop" + end, } @@ -100,14 +112,17 @@ instance = SolrWrapper.instance(options) case command when 'clean' - if instance.started? - $stderr.puts "Please stop solr before cleaning" - exit 1 - end - $stderr.puts "cleaning #{instance.instance_dir}..." - instance.remove_instance_dir! + $stderr.puts "cleaning solr instance: #{instance.instance_dir} ..." + instance.clean! +when 'purge' + $stderr.puts "cleaning solr downloads and solr instance: #{instance.instance_dir} ..." + instance.clean!(clean_downloads: true) when 'dir' puts instance.instance_dir +when 'status' + puts instance.status_info +when 'stop' + instance.stop else $stderr.print "Starting Solr #{instance.version} on port #{instance.port} ... " instance.wrap do |conn| diff --git a/lib/solr_wrapper/instance.rb b/lib/solr_wrapper/instance.rb index 770dd52..fd991c4 100644 --- a/lib/solr_wrapper/instance.rb +++ b/lib/solr_wrapper/instance.rb @@ -75,7 +75,7 @@ def start exec('start', p: port, c: config.cloud) # Wait for solr to start - unless status + unless started? sleep config.poll_interval end @@ -101,22 +101,32 @@ def restart end ## - # Check the status of a managed Solr service - def status - return true unless config.managed? + # Is Solr running? + def started? + status_info.include? "running on port #{port}" + end - out = exec('status').read - out =~ /running on port #{port}/ - rescue - false + ## + # Check the status of a Solr service + alias status started? + + ## + # Get the status information for a Solr service + def status_info + exec('status').read + rescue => err + [ + 'No status information available', + "message: #{err.message}", + "stack trace: #{err.backtrace}" + ].join("\n") end def pid return unless config.managed? @pid ||= begin - out = exec('status').read - out.match(/process (?\d+) running on port #{port}/) do |m| + status_info.match(/process (?\d+) running on port #{port}/) do |m| m[:pid].to_i end end @@ -124,14 +134,8 @@ def pid nil end - ## - # Is Solr running? - def started? - !!status - end - def wait - while (Process.getpgid(pid) rescue status) + while (Process.getpgid(pid) rescue started?) sleep config.poll_interval end end @@ -222,14 +226,16 @@ def with_collection(options = {}) end ## - # Clean up any files solr_wrapper may have downloaded - def clean! + # Clean up any files solr_wrapper manages + def clean!(clean_downloads: false) stop remove_instance_dir! - FileUtils.remove_entry(config.download_dir, true) if File.exist?(config.download_dir) FileUtils.remove_entry(config.tmp_save_dir, true) if File.exist? config.tmp_save_dir - md5.clean! - FileUtils.remove_entry(config.version_file) if File.exist? config.version_file + FileUtils.remove_entry(config.version_file) if File.exist?(config.version_file) + if clean_downloads + md5.clean! + FileUtils.remove_file(config.solr_zip_path, true) + end end ## diff --git a/lib/solr_wrapper/md5.rb b/lib/solr_wrapper/md5.rb index 12485cb..bade0d4 100644 --- a/lib/solr_wrapper/md5.rb +++ b/lib/solr_wrapper/md5.rb @@ -6,7 +6,7 @@ def initialize(config) end def clean! - FileUtils.remove_entry(config.md5sum_path) if File.exist? config.md5sum_path + FileUtils.remove_file(config.md5sum_path, true) end def validate?(file)