Class: Spotlight::ReindexJob

Inherits:
ApplicationJob show all
Includes:
JobTracking, LimitConcurrency
Defined in:
app/jobs/spotlight/reindex_job.rb

Overview

Reindex the given resources or exhibits

Constant Summary

Constants included from LimitConcurrency

LimitConcurrency::VALIDITY_TOKEN_PARAMETER

Instance Method Summary collapse

Methods included from JobTracking

#finalize_job_tracker!, #initialize_job_tracker!, #job_tracker, #mark_job_as_failed!

Instance Method Details

#exhibitObject

rubocop:enable Metrics/AbcSize, Metrics/MethodLength



63
64
65
66
67
68
69
70
71
72
# File 'app/jobs/spotlight/reindex_job.rb', line 63

def exhibit
  exhibit_or_resources = arguments.first

  case exhibit_or_resources
  when Spotlight::Exhibit
    exhibit_or_resources
  when Spotlight::Resource
    exhibit_or_resources.exhibit
  end
end

#perform(exhibit_or_resources, start: nil, finish: nil) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/jobs/spotlight/reindex_job.rb', line 26

def perform(exhibit_or_resources, start: nil, finish: nil, **)
  errors = 0

  error_handler = lambda do |pipeline, exception, _data|
    job_tracker.append_log_entry(
      type: :error,
      exhibit: exhibit,
      message: exception.to_s,
      backtrace: exception.backtrace.first(5).join("\n"),
      resource_id: (pipeline.source.id if pipeline.source.respond_to?(:id))
    )
    mark_job_as_failed!
    errors += 1
  end

  resource_list(exhibit_or_resources, start: start, finish: finish).each do |resource|
    resource.reindex(touch: false, commit: false, job_tracker: job_tracker, additional_data: job_data, on_error: error_handler) do |*|
      progress&.increment
    end
  rescue StandardError => e
    error_handler.call(Struct.new(:source).new(resource), e, nil)
  end

  job_tracker.append_log_entry(
    type: :summary,
    exhibit: exhibit,
    message: I18n.t(
      'spotlight.job_trackers.show.messages.status.in_progress',
      progress: progress.progress,
      total: progress.total,
      errors: (I18n.t('spotlight.job_trackers.show.messages.errors', count: errors) if errors.positive?)
    ),
    progress: progress.progress, total: progress.total, errors: errors
  )
end