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)
  • :verbose (Boolean)
  • :managed (Boolean)
  • :ignore_md5sum (Boolean)


27
28
29
# File 'lib/solr_wrapper/instance.rb', line 27

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

Instance Method Details

#clean! ⇒ Object

Clean up any files solr_wrapper may have downloaded



122
123
124
125
126
127
128
129
# File 'lib/solr_wrapper/instance.rb', line 122

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)


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

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



96
97
98
# File 'lib/solr_wrapper/instance.rb', line 96

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

#port ⇒ Object

Get the port this Solr instance is running at



116
117
118
# File 'lib/solr_wrapper/instance.rb', line 116

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

#start ⇒ Object

Start Solr and wait for it to become available



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/solr_wrapper/instance.rb', line 40

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)


76
77
78
# File 'lib/solr_wrapper/instance.rb', line 76

def started?
  !!status
end

#status ⇒ Object

Check the status of a managed Solr service



67
68
69
70
71
72
# File 'lib/solr_wrapper/instance.rb', line 67

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



54
55
56
57
58
59
60
61
62
63
# File 'lib/solr_wrapper/instance.rb', line 54

def stop
  if managed? and started?

    exec('stop', p: port)
    # Wait for solr to stop
    while status
      sleep 1
    end
  end
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)


105
106
107
108
109
110
111
112
# File 'lib/solr_wrapper/instance.rb', line 105

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

#wrap(&block) ⇒ Object



31
32
33
34
35
36
# File 'lib/solr_wrapper/instance.rb', line 31

def wrap &block
  start
  yield self
ensure
  stop
end