Commit 17f9162bb6283e2cfa40960a67d1e70c6f58892e
1 parent
9155938d
Exists in
routing_queue
Fixed deletes with routing and callbacks - #1182
Showing
4 changed files
with
24 additions
and
3 deletions
Show diff stats
CHANGELOG.md
lib/searchkick/process_batch_job.rb
... | ... | @@ -3,6 +3,10 @@ module Searchkick |
3 | 3 | queue_as { Searchkick.queue_name } |
4 | 4 | |
5 | 5 | def perform(class_name:, record_ids:) |
6 | + # separate routing from id | |
7 | + routing = Hash[record_ids.map { |r| r.split("|", 2) }] | |
8 | + record_ids = routing.keys | |
9 | + | |
6 | 10 | klass = class_name.constantize |
7 | 11 | scope = Searchkick.load_records(klass, record_ids) |
8 | 12 | scope = scope.search_import if scope.respond_to?(:search_import) |
... | ... | @@ -10,7 +14,16 @@ module Searchkick |
10 | 14 | |
11 | 15 | # determine which records to delete |
12 | 16 | 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 } | |
17 | + delete_records = delete_ids.map do |id| | |
18 | + m = klass.new | |
19 | + m.id = id | |
20 | + if routing[id] | |
21 | + m.define_singleton_method(:search_routing) do | |
22 | + routing[id] | |
23 | + end | |
24 | + end | |
25 | + m | |
26 | + end | |
14 | 27 | |
15 | 28 | # bulk reindex |
16 | 29 | index = klass.searchkick_index | ... | ... |
lib/searchkick/record_indexer.rb
... | ... | @@ -20,7 +20,14 @@ module Searchkick |
20 | 20 | raise Searchkick::Error, "Partial reindex not supported with queue option" |
21 | 21 | end |
22 | 22 | |
23 | - index.reindex_queue.push(record.id.to_s) | |
23 | + # always pass routing in case record is deleted | |
24 | + # before the queue job runs | |
25 | + if record.respond_to?(:search_routing) | |
26 | + routing = record.search_routing | |
27 | + end | |
28 | + | |
29 | + value = [record.id.to_s, routing].join("|") | |
30 | + index.reindex_queue.push(value) | |
24 | 31 | when :async |
25 | 32 | unless defined?(ActiveJob) |
26 | 33 | raise Searchkick::Error, "Active Job not found" | ... | ... |
test/routing_test.rb
... | ... | @@ -31,7 +31,7 @@ class RoutingTest < Minitest::Test |
31 | 31 | end |
32 | 32 | |
33 | 33 | def test_routing_queue |
34 | - skip # unless defined?(ActiveJob) && defined?(Redis) | |
34 | + skip unless defined?(ActiveJob) && defined?(Redis) | |
35 | 35 | |
36 | 36 | with_options(Song, routing: true, callbacks: :queue) do |
37 | 37 | store_names ["Dollar Tree"], Song | ... | ... |