class FcrepoWrapper::Instance
Attributes
Public Class Methods
@param [Hash] options @option options [String] :url @option options [String] :instance_dir Directory to store the fcrepo index files @option options [String] :version Fcrepo version to download and install @option options [String] :port port to run fcrepo on @option options [String] :version_file Local path to store the currently installed version @option options [String] :download_dir Local directory to store the downloaded fcrepo jar and its md5 file in (overridden by :download_path) @option options [String] :download_path Local path for storing the downloaded fcrepo jar file @option options [Boolean] :validate Should fcrepo_wrapper download a new md5 and (re-)false the zip file? (default: trueF) @option options [String] :md5sum Path/URL to MD5
checksum @option options [Boolean] :verbose return verbose info when running fcrepo commands @option options [Boolean] :ignore_md5sum @option options [Hash] :fcrepo_options @option options [Hash] :env
# File lib/fcrepo_wrapper/instance.rb, line 31 def initialize(options = {}) @config = Settings.new(Configuration.new(options)) end
Public Instance Methods
Clean up any files fcrepo_wrapper may have downloaded
# File lib/fcrepo_wrapper/instance.rb, line 154 def clean! stop remove_instance_dir! FileUtils.remove_entry(config.download_path) if File.exists?(config.download_path) FileUtils.remove_entry(config.tmp_save_dir, true) if File.exists? config.tmp_save_dir md5.clean! FileUtils.remove_entry(config.version_file) if File.exists? config.version_file end
# File lib/fcrepo_wrapper/instance.rb, line 169 def configure raise_error_unless_extracted end
extract a copy of fcrepo to instance_dir
Does noting if fcrepo already exists at instance_dir
@return [String] instance_dir
Directory where fcrepo has been installed
# File lib/fcrepo_wrapper/instance.rb, line 182 def extract return config.instance_dir if extracted? jar_file = download FileUtils.mkdir_p config.instance_dir FileUtils.cp jar_file, config.binary_path self.extracted_version = config.version config.instance_dir end
# File lib/fcrepo_wrapper/instance.rb, line 173 def extract_and_configure instance_dir = extract configure instance_dir end
Get the host this fcrepo instance is bound to
# File lib/fcrepo_wrapper/instance.rb, line 126 def host '127.0.0.1' end
# File lib/fcrepo_wrapper/instance.rb, line 144 def instance_dir config.instance_dir end
# File lib/fcrepo_wrapper/instance.rb, line 35 def md5 @md5 ||= MD5.new(config) end
# File lib/fcrepo_wrapper/instance.rb, line 148 def options config.options end
# File lib/fcrepo_wrapper/instance.rb, line 130 def port config.port end
# File lib/fcrepo_wrapper/instance.rb, line 47 def process_arguments ["java"] + config.java_options + config.fcrepo_options.merge(port: port) .map { |k, v| ["--#{k}", "#{v}"].reject(&:empty?) }.flatten end
Clean up any files in the fcrepo instance dir
# File lib/fcrepo_wrapper/instance.rb, line 165 def remove_instance_dir! FileUtils.remove_entry(config.instance_dir, true) if File.exists? config.instance_dir end
Stop fcrepo and wait for it to finish exiting
# File lib/fcrepo_wrapper/instance.rb, line 86 def restart if config.managed? && started? stop start end end
Start fcrepo and wait for it to become available
# File lib/fcrepo_wrapper/instance.rb, line 55 def start extract_and_configure if config.managed? @pid = spawn(config.env, *process_arguments) # Wait for fcrepo to start while !status sleep 1 end end end
Is fcrepo running?
# File lib/fcrepo_wrapper/instance.rb, line 120 def started? !!status end
Check the status of a managed fcrepo service
# File lib/fcrepo_wrapper/instance.rb, line 95 def status return true unless config.managed? return false if pid.nil? begin Process.getpgid(pid) rescue Errno::ESRCH return false end begin TCPSocket.new(host, port).close Net::HTTP.start(host, port) do |http| http.request(Net::HTTP::Get.new('/')) end true rescue Errno::ECONNREFUSED, Errno::EINVAL false end end
Stop fcrepo and wait for it to finish exiting
# File lib/fcrepo_wrapper/instance.rb, line 69 def stop if config.managed? && started? Process.kill 'HUP', pid # Wait for fcrepo to stop while status sleep 1 end Process.waitpid(pid) end @pid = nil end
Get a (likely) URL to the fcrepo instance
# File lib/fcrepo_wrapper/instance.rb, line 136 def url "http://#{host}:#{port}/" end
# File lib/fcrepo_wrapper/instance.rb, line 140 def version config.version end
# File lib/fcrepo_wrapper/instance.rb, line 39 def wrap(&_block) extract_and_configure start yield self ensure stop end
Protected Instance Methods
# File lib/fcrepo_wrapper/instance.rb, line 201 def download unless File.exists?(config.download_path) && md5.validate?(config.download_path) Downloader.fetch_with_progressbar config.download_url, config.download_path md5.validate! config.download_path end config.download_path end
rubocop:enable Lint/RescueException
# File lib/fcrepo_wrapper/instance.rb, line 197 def extracted? File.exists?(config.binary_path) && extracted_version == config.version end
Private Instance Methods
# File lib/fcrepo_wrapper/instance.rb, line 211 def extracted_version File.read(config.version_file).strip if File.exists? config.version_file end
# File lib/fcrepo_wrapper/instance.rb, line 215 def extracted_version=(version) File.open(config.version_file, "w") do |f| f.puts version end end
# File lib/fcrepo_wrapper/instance.rb, line 221 def raise_error_unless_extracted raise RuntimeError, "there is no fcrepo instance at #{config.instance_dir}. Run FcrepoWrapper.extract first." unless extracted? end