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.



15
16
17
# File 'lib/solr_wrapper/instance.rb', line 15

def initialize options = {}
  @options = options
end

Instance Attribute Details

#optionsObject (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

#create(options = {}) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
# File 'lib/solr_wrapper/instance.rb', line 157

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

  IO.popen([solr_binary, "create", "-c", options[:name], "-d", options[:dir], "-p", port, err: [:child, :out]]) do |io|
    if verbose?
      IO.copy_stream(io,$stderr)
    end
  end

  options[:name]
end

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



169
170
171
172
173
174
175
# File 'lib/solr_wrapper/instance.rb', line 169

def delete name, options = {}
  IO.popen([solr_binary, "delete", "-c", name, "-p", port, err: [:child, :out]]) do |io|
    if verbose?
      IO.copy_stream(io,$stderr)
    end
  end
end

#downloadObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/solr_wrapper/instance.rb', line 131

def download
  unless File.exists? download_path and md5sum(download_path) == expected_md5sum
    pbar = ProgressBar.create(title: File.basename(url), total: nil, format: "%t: |%B| %p%% (%e )")
    open(url, content_length_proc: lambda {|t|
      if t && 0 < t
        pbar.total = t
      end
      },
      progress_proc: lambda {|s|
        pbar.progress = s
      }) do |io|
      IO.copy_stream(io,download_path)
    end

    unless md5sum(download_path) == expected_md5sum
      raise "MD5 mismatch" unless options[:ignore_md5sum]
    end
  end

  download_path
end

#extractObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/solr_wrapper/instance.rb', line 98

def extract
  return solr_dir if File.exists?(solr_binary) and extracted_version == version

  zip_path = download

  begin
    Zip::File.open(zip_path) do |zip_file|
      # Handle entries one by one
      zip_file.each do |entry|
        dest_file = File.join(tmp_save_dir,entry.name)
        FileUtils.remove_entry(dest_file,true)
        entry.extract(dest_file)
      end
    end

  rescue Exception => e
    abort "Unable to unzip #{zip_path} into #{tmp_save_dir}: #{e.message}"
  end

  begin  
    FileUtils.remove_dir(solr_dir,true)
    FileUtils.cp_r File.join(tmp_save_dir, File.basename(default_url, ".zip")), solr_dir
    self.extracted_version = version
    FileUtils.chmod 0755, solr_binary
  rescue Exception => e
    abort "Unable to copy #{tmp_save_dir} to #{solr_dir}: #{e.message}"
  end

  solr_dir
ensure
  FileUtils.remove_entry tmp_save_dir if File.exists? tmp_save_dir
end

#portObject



153
154
155
# File 'lib/solr_wrapper/instance.rb', line 153

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

#startObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/solr_wrapper/instance.rb', line 26

def start
  extract
  IO.popen([solr_binary, "start", "-p", port, err: [:child, :out]]) do |io|
    stringio = StringIO.new
    if verbose?
      IO.copy_stream(io,$stderr)
    else
      IO.copy_stream(io, stringio)
    end
    _, exit_status = Process.wait2(io.pid)
    if exit_status != 0
      stringio.rewind
      raise "Unable to start solr: #{stringio.read}"
    end
  end if managed?

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

#started?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/solr_wrapper/instance.rb', line 94

def started?
  !!status
end

#statusObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/solr_wrapper/instance.rb', line 73

def status
  return true unless managed?

  stringio = StringIO.new

  IO.popen([solr_binary, "status", "-p", port, err: [:child, :out]]) do |io|
    IO.copy_stream(io, stringio)

    _, exit_status = Process.wait2(io.pid)

    stringio.rewind

    if exit_status != 0
      raise "Unable to query solr status: #{stringio.read}"
    end
  end

  out = stringio.read
  out =~ /running on port #{port}/
end

#stopObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/solr_wrapper/instance.rb', line 49

def stop
  return unless started?

  IO.popen([solr_binary, "stop", "-p", port, err: [:child, :out]]) do |io|
    stringio = StringIO.new
    if verbose?
      IO.copy_stream(io,$stderr)
    else
      IO.copy_stream(io, stringio)
    end
    _, exit_status = Process.wait2(io.pid)

    if exit_status != 0
      stringio.rewind
      raise "Unable to start solr: #{stringio.read}"
    end

    # Wait for solr to stop
    while status
      sleep 1
    end
  end if managed?
end

#with_collection(options = {}) ⇒ Object



177
178
179
180
181
182
183
184
# File 'lib/solr_wrapper/instance.rb', line 177

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

#wrap(&block) ⇒ Object



19
20
21
22
23
24
# File 'lib/solr_wrapper/instance.rb', line 19

def wrap &block
  start
  yield self
ensure
  stop
end