11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'app/workers/good_job/elasticsearch_process_queue.rb', line 11
def perform(index_class_name, index_name)
starting_time = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
index_klass = index_class_name.constantize
limit = 100
is_keyword_search = index_name.include?("keyword")
reindex_queue = is_keyword_search ? index_klass.keyword_reindex_queue : index_klass.reindex_queue
records = reindex_queue.reserve(limit: limit)
if records.any?
record_ids = records.map { |record| record[:record_id] }
key = records.map { |record| record[:key] }.uniq
routing_map = records.each_with_object({}) { |record, h| h[record[:record_id]] = record[:routing_key] }
batch = new_batch(on_success: true)
batch.properties[:index_class_name] = index_class_name
batch.properties[:record_ids_count] = record_ids.size
batch.properties[:key] = key
batch.properties[:starting_time] = starting_time
batch.add do
GoodJob::ElasticsearchProcessBatch.perform_later(index_klass.name, index_name, record_ids, routing_map) if ::Searchkick.client.indices.exists_alias?(name: index_name)
end
batch.enqueue
if record_ids.size == limit
GoodJob::ElasticsearchProcessQueue.perform_later(index_class_name, index_name)
end
end
end
|