Class: Sidekiq::ElasticsearchReindex

Inherits:
Object
  • Object
show all
Includes:
Worker
Defined in:
app/workers/sidekiq/elasticsearch_reindex.rb

Instance Method Summary collapse

Instance Method Details

#on_success(_status, options) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/workers/sidekiq/elasticsearch_reindex.rb', line 8

def on_success(_status, options)
  index_name = options["index_name"]
  index_class_name = options["index_class_name"]
  is_keyword_index = options["is_keyword_index"]
  start_time = options["start_time"]
  index_class = index_class_name.constantize
  reindex_queue = is_keyword_index ? index_class.keyword_reindex_queue : index_class.reindex_queue

  index_class.promote(index_name)
  # Index all records updated during the reindex
  index_class.active_record.where("updated_at >= ?", start_time).find_each do |record|
    reindex_queue.push(record.id.to_s)
  end
  ::Sidekiq::ElasticsearchProcessQueue.perform_async(index_class.name, index_name)

  index_class.update_settings(index_name, index: { refresh_interval: "1s" })
end

#perform(index_class_name, index_name) ⇒ Object



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
# File 'app/workers/sidekiq/elasticsearch_reindex.rb', line 26

def perform(index_class_name, index_name)
  index_class = index_class_name.constantize

  logger.info "Reindexing #{index_class_name}"

  options = { index_class_name: index_class_name, index_name: index_name, is_keyword_index: index_name.include?("keyword"), start_time: Time.current }

  batch = Sidekiq::Batch.new
  batch.on(:success, Sidekiq::ElasticsearchReindex, **options)
  batch.jobs do
    Cursor.execute(name: index_name, sql: index_class.active_record.select(:id).to_sql) do |cursor|
      while (records = cursor.next(100)) do
        record_ids = records.map { |record| record["id"] }
        Sidekiq::Client.push("class" => Sidekiq::ElasticsearchProcessBatch, "args" => [index_class_name, index_name, record_ids], "queue" => "elasticsearch_reindex")
      end
    end
  end

  # https://github.com/mperham/sidekiq/issues/2700
  ::HasUtils::Env.run_on_environment(::HasUtils::Env::TEST) do
    if Sidekiq::Testing.inline?
      stringified_options = options.map { |k, v| [k.to_s, v.to_s] }.to_h
      on_success("status", stringified_options)
    end
  end
end