# Helper-Klasse für das Handling von Testdaten im SOLR require 'timeout' require "yaml" class SolrTestdataProvider SOLR_TEST_INSTANCE_URL = "http://localhost:9983/solr" SOLR_TEST_INSTANCE_START_SCRIPT = File.expand_path(File.join(File.dirname(__FILE__), '..', 'test-solr', 'start-test-solr.sh')) TESTDATA_FILE = File.join(File.dirname(__FILE__), 'fixtures', 'solr-testdata.yml') class << self def load_testdata_into_solr core_name='core-01' runtime = Benchmark.realtime do con = solr_connection core_name # Löschen eventuell vorhandener alter Daten con.delete_by_query '*:*' print "Read testdata from #{TESTDATA_FILE} ... " docs = YAML.load_file TESTDATA_FILE print "send data to Solr #{con.uri.to_s} ..." con.add docs con.commit puts "ok." end puts "load solr-testdata finished in #{runtime}s" end # Starten der Test-Solr-Instanze falls diese noch nicht läuft def start_test_solr_instance unless solr_test_instance_running? # Solr-Testinstance läuft nicht => also diese jetzt starten start_cmd = SOLR_TEST_INSTANCE_START_SCRIPT print "Starte Test-Solr-Instance with #{start_cmd} .." $solr_test_instance_process = IO.popen(start_cmd) Timeout::timeout(120) do begin sleep 0.5 print '.' end while(!solr_test_instance_running?) end puts "ok" end end def stop_test_solr_instance if $solr_test_instance_process print "\nStopping Test-Solr-Instance.." # Java-Process(Jetty) als Child des gestarteten Scripts ermitteln ps_res = `ps -o pid h --ppid #{$solr_test_instance_process.pid}` child_pid = ps_res.to_i Process.kill("TERM", child_pid) if child_pid > 0 $solr_test_instance_process = nil puts "ok" end end private # Testen ob Test-Instance schon läuft def solr_test_instance_running? begin con = solr_connection 'core-01' con.head("admin/ping").response[:status] == 200 rescue => ex #puts ex.message return false end end def solr_connection core_name=nil url = SOLR_TEST_INSTANCE_URL url = "#{SOLR_TEST_INSTANCE_URL}/#{core_name}" if core_name MultiSolr.logger.debug("solr_connection: url==#{url}") RSolr.connect :url => url end end end