Class: SolrWrapper::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/solr_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)
  • :version (String)
  • :port (String)
  • :version_file (String)
  • :instance_dir (String)
  • :download_path (String)
  • :md5sum (String)
  • :solr_xml (String)
  • :verbose (Boolean)
  • :managed (Boolean)
  • :ignore_md5sum (Boolean)
  • :solr_options (Hash)
  • :env (Hash)


30
31
32
# File 'lib/solr_wrapper/instance.rb', line 30

def initialize(options = {})
  @options = options
end

Instance Attribute Details

#options ⇒ Object (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/solr_wrapper/instance.rb', line 13

def options
  @options
end

#pid ⇒ Object (readonly)

Returns the value of attribute pid.



13
14
15
# File 'lib/solr_wrapper/instance.rb', line 13

def pid
  @pid
end

Instance Method Details

#clean! ⇒ Object

Clean up any files solr_wrapper may have downloaded



127
128
129
130
131
132
133
134
# File 'lib/solr_wrapper/instance.rb', line 127

def clean!
  stop
  FileUtils.remove_entry(download_path) if File.exists? download_path
  FileUtils.remove_entry(tmp_save_dir, true) if File.exists? tmp_save_dir
  FileUtils.remove_entry(instance_dir, true) if File.exists? instance_dir
  FileUtils.remove_entry(md5sum_path) if File.exists? md5sum_path
  FileUtils.remove_entry(version_file) if File.exists? version_file
end

#create(options = {}) ⇒ Object

Create a new collection in solr

Parameters:

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

Options Hash (options):

  • :name (String)
  • :dir (String)


90
91
92
93
94
95
96
# File 'lib/solr_wrapper/instance.rb', line 90

def create(options = {})
  options[:name] ||= SecureRandom.hex

  exec("create", c: options[:name], d: options[:dir], p: port)

  options[:name]
end

#delete(name, _options = {}) ⇒ Object

Create a new collection in solr

Parameters:

  • name (String) —

    collection name



101
102
103
# File 'lib/solr_wrapper/instance.rb', line 101

def delete(name, _options = {})
  exec("delete", c: name, p: port)
end

#port ⇒ Object

Get the port this Solr instance is running at



121
122
123
# File 'lib/solr_wrapper/instance.rb', line 121

def port
  options.fetch(:port, "8983").to_s
end

#start ⇒ Object

Start Solr and wait for it to become available



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/solr_wrapper/instance.rb', line 43

def start
  extract
  if managed?
    exec('start', p: port)

    # Wait for solr to start
    unless status
      sleep 1
    end
  end
end

#started? ⇒ Boolean

Is Solr running?

Returns:

  • (Boolean)


81
82
83
# File 'lib/solr_wrapper/instance.rb', line 81

def started?
  !!status
end

#status ⇒ Object

Check the status of a managed Solr service



72
73
74
75
76
77
# File 'lib/solr_wrapper/instance.rb', line 72

def status
  return true unless managed?

  out = exec('status', output: true).read
  out =~ /running on port #{port}/
end

#stop ⇒ Object

Stop Solr and wait for it to finish exiting



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/solr_wrapper/instance.rb', line 57

def stop
  if managed? && started?

    exec('stop', p: port)
    # Wait for solr to stop
    while status
      sleep 1
    end
  end

  @pid = nil
end

#url ⇒ Object

Get a (likely) URL to the solr instance



138
139
140
# File 'lib/solr_wrapper/instance.rb', line 138

def url
  "http://127.0.0.1:#{port}/solr/"
end

#with_collection(options = {}) ⇒ Object

Create a new collection, run the block, and then clean up the collection

Parameters:

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

Options Hash (options):

  • :name (String)
  • :dir (String)


110
111
112
113
114
115
116
117
# File 'lib/solr_wrapper/instance.rb', line 110

def with_collection(options = {})
  name = create(options)
  begin
    yield name
  ensure
    delete name
  end
end

#wrap(&_block) ⇒ Object



34
35
36
37
38
39
# File 'lib/solr_wrapper/instance.rb', line 34

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