Class: SolrWrapper::Instance
- Inherits:
-
Object
- Object
- SolrWrapper::Instance
- Defined in:
- lib/solr_wrapper/instance.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
-
#clean! ⇒ Object
Clean up any files solr_wrapper may have downloaded.
- #configure ⇒ Object
-
#create(options = {}) ⇒ Object
Create a new collection in solr.
-
#delete(name, _options = {}) ⇒ Object
Create a new collection in solr.
-
#downconfig(options = {}) ⇒ Object
Copy the collection configuration from zookeeper to a local directory.
-
#extract ⇒ String
extract a copy of solr to instance_dir Does noting if solr already exists at instance_dir.
- #extract_and_configure ⇒ Object
- #host ⇒ Object
-
#initialize(options = {}) ⇒ Instance
constructor
A new instance of Instance.
- #instance_dir ⇒ Object
- #pid ⇒ Object
- #port ⇒ Object
-
#remove_instance_dir! ⇒ Object
Clean up any files in the Solr instance dir.
-
#restart ⇒ Object
Stop Solr and wait for it to finish exiting.
-
#start ⇒ Object
Start Solr and wait for it to become available.
-
#started? ⇒ Boolean
Is Solr running?.
-
#status ⇒ Object
Check the status of a managed Solr service.
-
#stop ⇒ Object
Stop Solr and wait for it to finish exiting.
-
#upconfig(options = {}) ⇒ Object
Update the collection configuration in zookeeper.
- #url ⇒ Object
- #version ⇒ Object
- #wait ⇒ Object
-
#with_collection(options = {}) ⇒ Object
Create a new collection, run the block, and then clean up the collection.
- #wrap(&_block) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Instance
Returns a new instance of Instance.
37 38 39 |
# File 'lib/solr_wrapper/instance.rb', line 37 def initialize( = {}) @config = Settings.new(Configuration.new()) end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
16 17 18 |
# File 'lib/solr_wrapper/instance.rb', line 16 def config @config end |
Instance Method Details
#clean! ⇒ Object
Clean up any files solr_wrapper may have downloaded
225 226 227 228 229 230 231 232 |
# File 'lib/solr_wrapper/instance.rb', line 225 def clean! stop remove_instance_dir! FileUtils.remove_entry(config.download_dir, true) if File.exist?(config.download_dir) FileUtils.remove_entry(config.tmp_save_dir, true) if File.exist? config.tmp_save_dir checksum_validator.clean! FileUtils.remove_entry(config.version_file) if File.exist? config.version_file end |
#configure ⇒ Object
240 241 242 243 244 |
# File 'lib/solr_wrapper/instance.rb', line 240 def configure raise_error_unless_extracted FileUtils.cp config.solr_xml, File.join(config.instance_dir, 'server', 'solr', 'solr.xml') if config.solr_xml FileUtils.cp_r File.join(config.extra_lib_dir, '.'), File.join(config.instance_dir, 'server', 'solr', 'lib') if config.extra_lib_dir end |
#create(options = {}) ⇒ Object
Create a new collection in solr
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/solr_wrapper/instance.rb', line 143 def create( = {}) [:name] ||= SecureRandom.hex = { p: port } [:c] = [:name] if [:name] [:n] = [:config_name] if [:config_name] [:d] = [:dir] if [:dir] Retriable.retriable do raise "Not started yet" unless started? end # short-circuit if we're using persisted data with an existing core/collection return if [:persist] && [:c] && client.exists?([:c]) exec("create", ) [:name] end |
#delete(name, _options = {}) ⇒ Object
Create a new collection in solr
202 203 204 |
# File 'lib/solr_wrapper/instance.rb', line 202 def delete(name, = {}) exec("delete", c: name, p: port) end |
#downconfig(options = {}) ⇒ Object
Copy the collection configuration from zookeeper to a local directory
186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/solr_wrapper/instance.rb', line 186 def downconfig( = {}) [:name] ||= SecureRandom.hex [:zkhost] ||= zkhost = { downconfig: true, n: [:name] } [:d] = [:dir] if [:dir] [:z] = [:zkhost] if [:zkhost] exec 'zk', [:name] end |
#extract ⇒ String
extract a copy of solr to instance_dir Does noting if solr already exists at instance_dir
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/solr_wrapper/instance.rb', line 255 def extract return config.instance_dir if extracted? 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(config.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 #{config.tmp_save_dir}: #{e.}" end begin FileUtils.remove_dir(config.instance_dir, true) FileUtils.cp_r File.join(config.tmp_save_dir, File.basename(config.download_url, ".zip")), config.instance_dir self.extracted_version = config.version FileUtils.chmod 0755, config.solr_binary rescue Exception => e abort "Unable to copy #{config.tmp_save_dir} to #{config.instance_dir}: #{e.}" end config.instance_dir ensure FileUtils.remove_entry config.tmp_save_dir if File.exist? config.tmp_save_dir end |
#extract_and_configure ⇒ Object
246 247 248 |
# File 'lib/solr_wrapper/instance.rb', line 246 def extract_and_configure extract.tap { configure } end |
#host ⇒ Object
41 42 43 |
# File 'lib/solr_wrapper/instance.rb', line 41 def host config.host end |
#instance_dir ⇒ Object
53 54 55 |
# File 'lib/solr_wrapper/instance.rb', line 53 def instance_dir config.instance_dir end |
#pid ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/solr_wrapper/instance.rb', line 113 def pid return unless config.managed? @pid ||= begin out = exec('status').read out.match(/process (?<pid>\d+) running on port #{port}/) do |m| m[:pid].to_i end end rescue nil end |
#port ⇒ Object
45 46 47 |
# File 'lib/solr_wrapper/instance.rb', line 45 def port config.port end |
#remove_instance_dir! ⇒ Object
Clean up any files in the Solr instance dir
236 237 238 |
# File 'lib/solr_wrapper/instance.rb', line 236 def remove_instance_dir! FileUtils.remove_entry(instance_dir, true) if File.exist? instance_dir end |
#restart ⇒ Object
Stop Solr and wait for it to finish exiting
96 97 98 99 100 |
# File 'lib/solr_wrapper/instance.rb', line 96 def restart if config.managed? && started? exec('restart', p: port, c: config.cloud) end end |
#start ⇒ Object
Start Solr and wait for it to become available
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/solr_wrapper/instance.rb', line 71 def start extract_and_configure if config.managed? exec('start', p: port, c: config.cloud) # Wait for solr to start unless status sleep config.poll_interval end after_start end end |
#started? ⇒ Boolean
Is Solr running?
128 129 130 |
# File 'lib/solr_wrapper/instance.rb', line 128 def started? !!status end |
#status ⇒ Object
Check the status of a managed Solr service
104 105 106 107 108 109 110 111 |
# File 'lib/solr_wrapper/instance.rb', line 104 def status return true unless config.managed? out = exec('status').read out =~ /running on port #{port}/ rescue false end |
#stop ⇒ Object
Stop Solr and wait for it to finish exiting
87 88 89 90 91 92 |
# File 'lib/solr_wrapper/instance.rb', line 87 def stop if config.managed? && started? exec('stop', p: port) wait end end |
#upconfig(options = {}) ⇒ Object
Update the collection configuration in zookeeper
168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/solr_wrapper/instance.rb', line 168 def upconfig( = {}) [:name] ||= SecureRandom.hex [:zkhost] ||= zkhost = { upconfig: true, n: [:name] } [:d] = [:dir] if [:dir] [:z] = [:zkhost] if [:zkhost] exec 'zk', [:name] end |
#url ⇒ Object
49 50 51 |
# File 'lib/solr_wrapper/instance.rb', line 49 def url config.url end |
#version ⇒ Object
57 58 59 |
# File 'lib/solr_wrapper/instance.rb', line 57 def version config.version end |
#wait ⇒ Object
132 133 134 135 136 |
# File 'lib/solr_wrapper/instance.rb', line 132 def wait while (Process.getpgid(pid) rescue status) sleep config.poll_interval end end |
#with_collection(options = {}) ⇒ Object
Create a new collection, run the block, and then clean up the collection
211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/solr_wrapper/instance.rb', line 211 def with_collection( = {}) = config..merge() return yield if .empty? name = create() begin yield name ensure delete name unless [:persist] end end |
#wrap(&_block) ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/solr_wrapper/instance.rb', line 61 def wrap(&_block) extract_and_configure start yield self ensure stop end |