Commit ac938b363e613acae46b3424b2bb040bc33c6abf

Authored by Andrew Kane
1 parent d92c36f5

Seperate ProcessBatchJob again for cleaner code

lib/searchkick/bulk_reindex_job.rb
@@ -2,7 +2,7 @@ module Searchkick @@ -2,7 +2,7 @@ module Searchkick
2 class BulkReindexJob < ActiveJob::Base 2 class BulkReindexJob < ActiveJob::Base
3 queue_as :searchkick 3 queue_as :searchkick
4 4
5 - def perform(class_name:, record_ids: nil, index_name: nil, method_name: nil, batch_id: nil, min_id: nil, max_id: nil, delete_missing: false) 5 + def perform(class_name:, record_ids: nil, index_name: nil, method_name: nil, batch_id: nil, min_id: nil, max_id: nil)
6 klass = class_name.constantize 6 klass = class_name.constantize
7 index = index_name ? Searchkick::Index.new(index_name) : klass.searchkick_index 7 index = index_name ? Searchkick::Index.new(index_name) : klass.searchkick_index
8 record_ids ||= min_id..max_id 8 record_ids ||= min_id..max_id
@@ -10,9 +10,7 @@ module Searchkick @@ -10,9 +10,7 @@ module Searchkick
10 Searchkick.load_records(klass, record_ids), 10 Searchkick.load_records(klass, record_ids),
11 method_name: method_name, 11 method_name: method_name,
12 batch: true, 12 batch: true,
13 - batch_id: batch_id,  
14 - delete_missing: delete_missing,  
15 - record_ids: record_ids 13 + batch_id: batch_id
16 ) 14 )
17 end 15 end
18 end 16 end
lib/searchkick/index.rb
@@ -247,14 +247,14 @@ module Searchkick @@ -247,14 +247,14 @@ module Searchkick
247 end 247 end
248 end 248 end
249 249
250 - def import_scope(scope, resume: false, method_name: nil, async: false, batch: false, batch_id: nil, full: false, delete_missing: false, record_ids: nil) 250 + def import_scope(scope, resume: false, method_name: nil, async: false, batch: false, batch_id: nil, full: false)
251 batch_size = @options[:batch_size] || 1000 251 batch_size = @options[:batch_size] || 1000
252 252
253 # use scope for import 253 # use scope for import
254 scope = scope.search_import if scope.respond_to?(:search_import) 254 scope = scope.search_import if scope.respond_to?(:search_import)
255 255
256 if batch 256 if batch
257 - import_or_update scope.to_a, method_name, async, delete_missing, record_ids, scope.model_name.name.constantize 257 + import_or_update scope.to_a, method_name, async
258 redis.srem(batches_key, batch_id) if batch_id && redis 258 redis.srem(batches_key, batch_id) if batch_id && redis
259 elsif full && async 259 elsif full && async
260 if scope.respond_to?(:primary_key) 260 if scope.respond_to?(:primary_key)
@@ -422,7 +422,7 @@ module Searchkick @@ -422,7 +422,7 @@ module Searchkick
422 end 422 end
423 end 423 end
424 424
425 - def import_or_update(records, method_name, async, delete_missing = false, record_ids = nil, klass = nil) 425 + def import_or_update(records, method_name, async)
426 if records.any? 426 if records.any?
427 if async 427 if async
428 Searchkick::BulkReindexJob.perform_later( 428 Searchkick::BulkReindexJob.perform_later(
@@ -433,22 +433,9 @@ module Searchkick @@ -433,22 +433,9 @@ module Searchkick
433 ) 433 )
434 else 434 else
435 records = records.select(&:should_index?) 435 records = records.select(&:should_index?)
436 -  
437 - delete_records =  
438 - if delete_missing  
439 - # determine which records to delete  
440 - (record_ids - records.map { |r| r.id.to_s }).map { |id| m = klass.new; m.id = id; m }  
441 - else  
442 - []  
443 - end  
444 -  
445 - with_retries do  
446 - # bulk reindex  
447 - possibly_bulk(delete_records.any?) do  
448 - if records.any?  
449 - method_name ? bulk_update(records, method_name) : import(records)  
450 - end  
451 - bulk_delete(delete_records) if delete_records.any? 436 + if records.any?
  437 + with_retries do
  438 + method_name ? bulk_update(records, method_name) : import(records)
452 end 439 end
453 end 440 end
454 end 441 end
@@ -473,18 +460,6 @@ module Searchkick @@ -473,18 +460,6 @@ module Searchkick
473 Searchkick.redis 460 Searchkick.redis
474 end 461 end
475 462
476 - # use bulk if no callbacks value set and deleted records  
477 - # if no deleted records, we can show friendlier notifications  
478 - def possibly_bulk(deleted_records)  
479 - if Searchkick.callbacks_value || !deleted_records  
480 - yield  
481 - else  
482 - Searchkick.callbacks(:bulk) do  
483 - yield  
484 - end  
485 - end  
486 - end  
487 -  
488 def batches_key 463 def batches_key
489 "searchkick:reindex:#{name}:batches" 464 "searchkick:reindex:#{name}:batches"
490 end 465 end
lib/searchkick/process_batch_job.rb
@@ -3,12 +3,21 @@ module Searchkick @@ -3,12 +3,21 @@ module Searchkick
3 queue_as :searchkick 3 queue_as :searchkick
4 4
5 def perform(class_name:, record_ids:) 5 def perform(class_name:, record_ids:)
6 - # job deprecated  
7 - Searchkick::BulkReindexJob.perform_now(  
8 - class_name: class_name,  
9 - record_ids: record_ids,  
10 - delete_missing: true  
11 - ) 6 + klass = class_name.constantize
  7 + scope = Searchkick.load_records(klass, record_ids)
  8 + scope = scope.search_import if scope.respond_to?(:search_import)
  9 + records = scope.select(&:should_index?)
  10 +
  11 + # determine which records to delete
  12 + delete_ids = record_ids - records.map { |r| r.id.to_s }
  13 + delete_records = delete_ids.map { |id| m = klass.new; m.id = id; m }
  14 +
  15 + # bulk reindex
  16 + index = klass.searchkick_index
  17 + Searchkick.callbacks(:bulk) do
  18 + index.bulk_index(records)
  19 + index.bulk_delete(delete_records)
  20 + end
12 end 21 end
13 end 22 end
14 end 23 end
lib/searchkick/process_queue_job.rb
@@ -8,10 +8,9 @@ module Searchkick @@ -8,10 +8,9 @@ module Searchkick
8 limit = model.searchkick_index.options[:batch_size] || 1000 8 limit = model.searchkick_index.options[:batch_size] || 1000
9 record_ids = Searchkick::ReindexQueue.new(model.searchkick_index.name).reserve(limit: limit) 9 record_ids = Searchkick::ReindexQueue.new(model.searchkick_index.name).reserve(limit: limit)
10 if record_ids.any? 10 if record_ids.any?
11 - Searchkick::BulkReindexJob.perform_later( 11 + Searchkick::ProcessBatchJob.perform_later(
12 class_name: model.name, 12 class_name: model.name,
13 - record_ids: record_ids,  
14 - delete_missing: true 13 + record_ids: record_ids
15 ) 14 )
16 # TODO when moving to reliable queuing, mark as complete 15 # TODO when moving to reliable queuing, mark as complete
17 16