Class: Dor::IndexingService

Inherits:
Object
  • Object
show all
Defined in:
lib/dor/services/indexing_service.rb

Constant Summary collapse

@@loggers =

memoize the loggers we create in a hash, init with a nil default logger

{ default: nil }

Class Method Summary collapse

Class Method Details

.default_index_loggerObject



20
21
22
# File 'lib/dor/services/indexing_service.rb', line 20

def self.default_index_logger
  @@loggers[:default] ||= generate_index_logger
end

.generate_index_logger { ... } ⇒ Object

Returns a Logger instance for recording info about indexing attempts

Yields:

  • attempt to execute ‘entry_id_block’ and use the result as an extra identifier for the log entry. a placeholder will be used otherwise. ‘request.uuid’ might be useful in a Rails app.



7
8
9
10
11
12
13
14
15
# File 'lib/dor/services/indexing_service.rb', line 7

def self.generate_index_logger(&entry_id_block)
  index_logger = Logger.new(Config.indexing_svc.log, Config.indexing_svc.log_rotation_interval)
  index_logger.formatter = proc do |severity, datetime, progname, msg|
    date_format_str = Config.indexing_svc.log_date_format_str
    entry_id = begin entry_id_block.call rescue '---' end
    "[#{entry_id}] [#{datetime.utc.strftime(date_format_str)}] #{msg}\n"
  end
  index_logger
end

.reindex_object(obj) ⇒ Object

takes a Dor object and indexes it to solr. doesn’t commit automatically.



25
26
27
28
29
# File 'lib/dor/services/indexing_service.rb', line 25

def self.reindex_object(obj)
  solr_doc = obj.to_solr
  Dor::SearchService.solr.add(solr_doc)
  solr_doc
end

.reindex_pid(pid, index_logger = nil, should_raise_errors = true) ⇒ Object

retrieves a single Dor object by pid, indexes the object to solr, does some logging (will use a defualt logger if one is not provided). doesn’t commit automatically.

WARNING/TODO: the tests indicate that the “rescue Exception” block at the end will get skipped, and the thrown exception (e.g. SystemStackError) will not be logged. since that’s the only consequence, and the exception bubbles up as we would want anyway, it doesn’t seem worth blocking refactoring. see github.com/sul-dlss/dor-services/issues/156 extra logging in this case would be nice, but centralized indexing that’s otherwise fully functional is nicer.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/dor/services/indexing_service.rb', line 40

def self.reindex_pid(pid, index_logger = nil, should_raise_errors = true)
  index_logger ||= default_index_logger
  obj = Dor.load_instance pid
  solr_doc = reindex_object obj
  index_logger.info "updated index for #{pid}"
  solr_doc
rescue StandardError => se
  if se.is_a? ActiveFedora::ObjectNotFoundError
    index_logger.warn "failed to update index for #{pid}, object not found in Fedora"
  else
    index_logger.warn "failed to update index for #{pid}, unexpected StandardError, see main app log: #{se.backtrace}"
  end
  raise se if should_raise_errors
rescue Exception => ex
  index_logger.error "failed to update index for #{pid}, unexpected Exception, see main app log: #{ex.backtrace}"
  raise ex # don't swallow anything worse than StandardError
end

.reindex_pid_list(pid_list, should_commit = false) ⇒ Object

given a list of pids, retrieve those objects from fedora, index each to solr, optionally commit



59
60
61
62
# File 'lib/dor/services/indexing_service.rb', line 59

def self.reindex_pid_list(pid_list, should_commit = false)
  pid_list.each { |pid| reindex_pid pid, nil, false } # use the default logger, don't let individual errors nuke the rest of the batch
  ActiveFedora.solr.conn.commit if should_commit
end