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, options = {}) ⇒ 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, options = {})
  solr_doc = obj.to_solr
  Dor::SearchService.solr.add(solr_doc, options)
  solr_doc
end

.reindex_pid(pid, index_logger, options = {}) ⇒ Object .reindex_pid(pid, index_logger, should_raise_errors, options = {}) ⇒ Object .reindex_pid(pid, options = {}) ⇒ Object

retrieves a single Dor object by pid, indexes the object to solr, does some logging (will use a default 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.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/dor/services/indexing_service.rb', line 44

def self.reindex_pid(pid, *args)
  options = {}
  options = args.pop if args.last.is_a? Hash

  if args.length > 0
    warn "Dor::IndexingService.reindex_pid with primitive arguments is deprecated; pass e.g. { logger: logger, raise_errors: bool } instead"
    index_logger, should_raise_errors = args
    index_logger ||= default_index_logger
    should_raise_errors = true if should_raise_errors.nil?
  else
    index_logger = options.fetch(:logger, default_index_logger)
    should_raise_errors = options.fetch(:raise_errors, true)
  end

  obj = Dor.load_instance pid
  solr_doc = reindex_object obj, options
  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



75
76
77
78
# File 'lib/dor/services/indexing_service.rb', line 75

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