Class: FcrepoWrapper::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/fcrepo_wrapper/instance.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Instance

Returns a new instance of Instance.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :url (String)
  • :instance_dir (String)

    Directory to store the fcrepo index files

  • :version (String)

    Fcrepo version to download and install

  • :port (String)

    port to run fcrepo on

  • :version_file (String)

    Local path to store the currently installed version

  • :download_dir (String)

    Local directory to store the downloaded fcrepo jar and its md5 file in (overridden by :download_path)

  • :download_path (String)

    Local path for storing the downloaded fcrepo jar file

  • :validate (Boolean)

    Should fcrepo_wrapper download a new md5 and (re-)false the zip file? (default: trueF)

  • :md5sum (String)

    Path/URL to MD5 checksum

  • :verbose (Boolean)

    return verbose info when running fcrepo commands

  • :ignore_md5sum (Boolean)
  • :fcrepo_options (Hash)
  • :env (Hash)


31
32
33
# File 'lib/fcrepo_wrapper/instance.rb', line 31

def initialize(options = {})
  @config = Settings.new(Configuration.new(options))
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



14
15
16
# File 'lib/fcrepo_wrapper/instance.rb', line 14

def config
  @config
end

#pidObject (readonly)

Returns the value of attribute pid.



14
15
16
# File 'lib/fcrepo_wrapper/instance.rb', line 14

def pid
  @pid
end

Instance Method Details

#clean!Object

Clean up any files fcrepo_wrapper may have downloaded



154
155
156
157
158
159
160
161
# 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

#configureObject



169
170
171
# File 'lib/fcrepo_wrapper/instance.rb', line 169

def configure
  raise_error_unless_extracted
end

#extractString

extract a copy of fcrepo to instance_dir Does noting if fcrepo already exists at instance_dir

Returns:

  • (String)

    instance_dir Directory where fcrepo has been installed



182
183
184
185
186
187
188
189
190
191
192
# 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

#extract_and_configureObject



173
174
175
176
177
# File 'lib/fcrepo_wrapper/instance.rb', line 173

def extract_and_configure
  instance_dir = extract
  configure
  instance_dir
end

#hostObject

Get the host this fcrepo instance is bound to



126
127
128
# File 'lib/fcrepo_wrapper/instance.rb', line 126

def host
  '127.0.0.1'
end

#instance_dirObject



144
145
146
# File 'lib/fcrepo_wrapper/instance.rb', line 144

def instance_dir
  config.instance_dir
end

#md5Object



35
36
37
# File 'lib/fcrepo_wrapper/instance.rb', line 35

def md5
  @md5 ||= MD5.new(config)
end

#optionsObject



148
149
150
# File 'lib/fcrepo_wrapper/instance.rb', line 148

def options
  config.options
end

#portObject



130
131
132
# File 'lib/fcrepo_wrapper/instance.rb', line 130

def port
  config.port
end

#process_argumentsObject



47
48
49
50
51
# 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

#remove_instance_dir!Object

Clean up any files in the fcrepo instance dir



165
166
167
# 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

#restartObject

Stop fcrepo and wait for it to finish exiting



86
87
88
89
90
91
# File 'lib/fcrepo_wrapper/instance.rb', line 86

def restart
  if config.managed? && started?
    stop
    start
  end
end

#startObject

Start fcrepo and wait for it to become available



55
56
57
58
59
60
61
62
63
64
65
# 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

#started?Boolean

Is fcrepo running?

Returns:

  • (Boolean)


120
121
122
# File 'lib/fcrepo_wrapper/instance.rb', line 120

def started?
  !!status
end

#statusObject

Check the status of a managed fcrepo service



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# 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

#stopObject

Stop fcrepo and wait for it to finish exiting



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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

#urlObject

Get a (likely) URL to the fcrepo instance



136
137
138
# File 'lib/fcrepo_wrapper/instance.rb', line 136

def url
  "http://#{host}:#{port}/"
end

#versionObject



140
141
142
# File 'lib/fcrepo_wrapper/instance.rb', line 140

def version
  config.version
end

#wrap(&_block) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/fcrepo_wrapper/instance.rb', line 39

def wrap(&_block)
  extract_and_configure
  start
  yield self
ensure
  stop
end