#!/usr/bin/env ruby require 'rubygems' require 'optparse' require 'stomp' options = {} optparse = OptionParser.new do|opts| opts.banner = "Usage: solrizer [options]" options[:hydra_home] = nil opts.on( '--hydra_home PATH', 'Load the Hydra instance at this path' ) do |path| if File.exist?(File.join(path,"config","environment.rb")) options[:hydra_home] = path else puts "#{path} does not appear to be a valid rails home" exit end end options[:port] = 61613 opts.on('-p','--port NUM', 'Stomp port') do |port| options[:port] = port end options[:host] = 'localhost' opts.on('-o','--host HOSTNAME', 'Host to connect to') do |host| options[:host] = host end options[:user] = 'fedoraStomper' opts.on('-u', '--user USERNAME', 'User name for stomp listener') do |user| options[:user] = user end options[:password] = 'fedoraStomper' opts.on('-w', '--password PASSWORD', 'Password for stomp listener') do |password| options[:password] = password end options[:destination] = '/topic/fedora.apim.update' opts.on('-d','--destination TOPIC', 'Topic to listen to') do |destination| options[:destination] = destination end opts.on('-h', '--help', 'Display this screen') do puts opts exit end end optparse.parse! begin; require 'rubygems'; rescue; end if options[:hydra_home] puts "Loading app..." Dir.chdir(options[:hydra_home]) require File.join(options[:hydra_home],"config","environment.rb") puts "app loaded" else $stderr.puts "The --hydra_home PATH option is mandatory. Please provide the path to the root of a valid Hydra instance." exit 1 end puts "loading listener" begin @port = options[:port] @host = options[:host] @user = options[:user] @password = options[:password] @reliable = true @clientid = "fedora_stomper" @destination = options[:destination] $stderr.print "Connecting to stomp://#{@host}:#{@port} as #{@user}\n" @conn = Stomp::Connection.open(@user, @password, @host, @port, @reliable, 5, {"client-id" => @clientid} ) $stderr.print "Getting output from #{@destination}\n" @conn.subscribe(@destination, {"activemq.subscriptionName" => @clientid, :ack =>"client" }) while true @msg = @conn.receive pid = @msg.headers["pid"] method = @msg.headers["methodName"] puts @msg.headers.inspect puts "\nPID: #{@msg.headers["pid"]}\n" if ["addDatastream", "addRelationship","ingest","modifyDatastreamByValue","modifyDatastreamByReference","modifyObject","purgeDatastream","purgeRelationship"].include? method ActiveFedora::Base.find(@msg.headers["pid"], cast: true).update_index elsif method == "purgeObject" ActiveFedora::SolrService.instance.conn.delete_by_id(pid) else $stderr.puts "Unknown Method: #{method}" end puts "updated solr index for #{@msg.headers["pid"]}\n" @conn.ack @msg.headers["message-id"] end @conn.join rescue Exception => e p e end